几件事。指针和原始类型返回不同的阵列成员
当我宣布我的结构与尺寸
3
的成员数组,那不是意味着数组有4个元素? 0,1,2,3?为什么他们,当我尝试插入字符A
,B
和C
时,它告诉我initializer-string for array of chars is too long [-fpermissive]
?#include <iostream> using namespace std; struct Student { double no; char grade[3]; }; int main() { struct Student harry = {975, "ABC"}; }
当我打印的字符数组的特定索引的地址我从下面的代码的结果如下:
struct Student { double no; char grade[4]; }; int main() { struct Student harry = {975, "ABC"}; for (int i = 0; i < 4; i++) cout << "h.g[" << i << "]" << harry.grade[i] << endl; for (int i = 0; i < 4; i++) cout << "h.g[" << i << "]" << &harry.grade[i] << endl; }
结果:
h.g[0]A
h.g[1]B
h.g[2]C
h.g[3]
&h.g[0]ABC
&h.g[1]BC
&h.g[2]C
&h.g[3]
为什么第一个索引打印ABC
,然后BC
等等每个字符的分开像第一个循环?
大小3意味着3个元素。真的很简单。如果它有4个元素,其大小将为4.“大小”并不意味着“最后一个元素的索引”,这意味着大小。 –