小编Bbv*_*ghe的帖子

在while循环中增加*char的指针

这是我有的:

char* input = new char [input_max]
char* inputPtr = iput;
Run Code Online (Sandbox Code Playgroud)

我想使用inputPtr遍历输入数组.但是我不确定什么会正确检查我是否到达了字符串的末尾:

while (*inputPtr++)
{
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

要么

while (*inputPtr != '\0')
{
    inputPtr++;
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

还是更优雅的选择?

c++ string pointers loops traversal

5
推荐指数
1
解决办法
2万
查看次数

C++将char指针设置为null

在Visual Studio 2012中,我正在搞乱指针,我意识到这个程序一直在崩溃:

    #include     <iostream>
    using std::cout;
    using std::endl;

    int main () 
   {

       const char* pointer = nullptr;

       cout << "This is the value of pointer   " << pointer << "." << endl;

       return 0;

   }
Run Code Online (Sandbox Code Playgroud)

我的意图是在设置pointernull,然后打印地址.即使程序编译,它也会在运行时崩溃.有人可以解释发生了什么吗?

另外,pointer和的区别是什么 *pointer

c++ pointers nullptr

3
推荐指数
1
解决办法
2125
查看次数

如果导入 iostream,为什么要使用命名空间

我是 C++ 的初学者,最近向我介绍了 std 之类的命名空间。但是,如果像 cout 和 endl 这样的函数是在 iostream 头文件中定义的,为什么还要包含 std 命名空间呢?或者这些函数实际上是在 std 命名空间中定义的吗?如果是这样,那么为什么要包含 iostream 文件?

c++ iostream std

2
推荐指数
2
解决办法
859
查看次数

fopen错误的文件字符串?

我的桌面上有一个名为fun的文本文件,但是当我通过时:

FILE* fp;

if((fp = fopen("/Users/<username>/Desktop/fun", "r")) == NULL)
{
    printf("File didn't open\n");
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

fp为null.我也试过了

/home/<username>/Desktop/fun
Run Code Online (Sandbox Code Playgroud)

和许多变化,我似乎仍然无法获得正确的文件路径.我是使用文件和C的新手.任何帮助将不胜感激.

c file-io file

2
推荐指数
1
解决办法
840
查看次数

头文件中的struct无法识别

practice.h

struct CandyBar
{
    string name;
    double weight;
    int calories;

};
Run Code Online (Sandbox Code Playgroud)

practice.cpp

#include    <iostream>
#include    <string>
#include    "practice.h"  

using namespace std;

int main()
{
    CandyBar snacks{ "Mocha Munch", 2.3, 350 };

    cout << snacks.name << "\t" << snacks.weight << "\t" << snacks.calories << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我构建解决方案时,我得到错误:

practice.h(5): error C2146: syntax error : missing ';' before identifier 'name'

practice.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2440: 'initializing' : cannot convert …
Run Code Online (Sandbox Code Playgroud)

c++ struct header-files

0
推荐指数
1
解决办法
3030
查看次数

标签 统计

c++ ×4

pointers ×2

c ×1

file ×1

file-io ×1

header-files ×1

iostream ×1

loops ×1

nullptr ×1

std ×1

string ×1

struct ×1

traversal ×1