这是我有的:
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)
还是更优雅的选择?
在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)
我的意图是在设置pointer到null,然后打印地址.即使程序编译,它也会在运行时崩溃.有人可以解释发生了什么吗?
另外,pointer和的区别是什么 *pointer
我是 C++ 的初学者,最近向我介绍了 std 之类的命名空间。但是,如果像 cout 和 endl 这样的函数是在 iostream 头文件中定义的,为什么还要包含 std 命名空间呢?或者这些函数实际上是在 std 命名空间中定义的吗?如果是这样,那么为什么要包含 iostream 文件?
我的桌面上有一个名为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的新手.任何帮助将不胜感激.
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)