C++ 字符出错?
C++ char getting errors?
#include <iostream>
int main()
{
char[] name = { "Nitish prajapati" };
char* namePointer = &name ;
std::cout << "\n name = " << name;
std::cout << "\n &name = " << &name;
std::cout << "\n &namePointer = " << &namePointer;
std::cout << "\n namePointer = " << namePointer;
return 0;
}
为什么这个程序报错:expected unqualified-id before '[' token
?
和
解释你如何实际使用 char 以及引用和取消引用(即指针)
应该是:
const char name[] = {"Nitish prajapati"};
const char* namePointer = name ;
这两种说法都是错误的
char[] name = { "Nitish prajapati" };
char* namePointer = &name ;
在 C++ 中,数组的有效声明类似于
char name[] = { "Nitish prajapati" };
至于第二条语句,则没有从类型 char ( * )[17]
到 char *
的隐式转换。
声明的初始值设定项的类型为 char ( * )[17]
,而声明的指针的类型为 char *
你应该写任何一个
char* namePointer = name ;
或
char ( *namePointer)[17] = &name ;
#include <iostream>
int main()
{
char[] name = { "Nitish prajapati" };
char* namePointer = &name ;
std::cout << "\n name = " << name;
std::cout << "\n &name = " << &name;
std::cout << "\n &namePointer = " << &namePointer;
std::cout << "\n namePointer = " << namePointer;
return 0;
}
为什么这个程序报错:expected unqualified-id before '[' token
?
和
解释你如何实际使用 char 以及引用和取消引用(即指针)
应该是:
const char name[] = {"Nitish prajapati"};
const char* namePointer = name ;
这两种说法都是错误的
char[] name = { "Nitish prajapati" };
char* namePointer = &name ;
在 C++ 中,数组的有效声明类似于
char name[] = { "Nitish prajapati" };
至于第二条语句,则没有从类型 char ( * )[17]
到 char *
的隐式转换。
声明的初始值设定项的类型为 char ( * )[17]
,而声明的指针的类型为 char *
你应该写任何一个
char* namePointer = name ;
或
char ( *namePointer)[17] = &name ;