指针和数组之间的区别[重复]

时间:2022-09-06 14:39:55

Possible Duplicates:
Difference between char *str=“STRING” and char str[] = “STRING” ?
C: differences between pointer and array

可能重复:char * str =“STRING”和char str [] =“STRING”之间的区别? C:指针和数组之间的差异

Hi,

Can anybody tell me the difference between the statements below?

谁能告诉我下面的陈述之间的区别?

char *p = "This is a test";

char a[] = "This is a test";

3 个解决方案

#1


7  

When you declare char p[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters i.e. "This is test" is copied to the elements in this array.

当你声明char p []时,你声明了一个chars数组(可以读取和写入),并且这个数组被初始化为一些字符序列,即“T​​his is test”被复制到这个数组中的元素。

When you declare char* p, you are declaring a pointer that points directly to some constant literal - not a copy. These can only be read.

当你声明char * p时,你声明一个直接指向某个常量文字的指针 - 而不是副本。这些只能被阅读。

#2


4  

a is an array which means that you can use the sizeof() operator on a and sizeof(a)/sizeof(a[0]) is equal to the array length.

a是一个数组,这意味着你可以在a和sizeof(a)/ sizeof(a [0])上使用sizeof()运算符等于数组长度。

p is a pointer to a constant memory zone.

p是指向常量内存区域的指针。

#3


3  

1 - pointer which points to the read only section of the program containing "This is test\0" string.

1 - 指向包含“This is test \ 0”字符串的程序的只读部分的指针。

2 - memory (13 bytes) which is initialized with contents mentioned above.

2 - 用上述内容初始化的存储器(13字节)。

#1


7  

When you declare char p[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters i.e. "This is test" is copied to the elements in this array.

当你声明char p []时,你声明了一个chars数组(可以读取和写入),并且这个数组被初始化为一些字符序列,即“T​​his is test”被复制到这个数组中的元素。

When you declare char* p, you are declaring a pointer that points directly to some constant literal - not a copy. These can only be read.

当你声明char * p时,你声明一个直接指向某个常量文字的指针 - 而不是副本。这些只能被阅读。

#2


4  

a is an array which means that you can use the sizeof() operator on a and sizeof(a)/sizeof(a[0]) is equal to the array length.

a是一个数组,这意味着你可以在a和sizeof(a)/ sizeof(a [0])上使用sizeof()运算符等于数组长度。

p is a pointer to a constant memory zone.

p是指向常量内存区域的指针。

#3


3  

1 - pointer which points to the read only section of the program containing "This is test\0" string.

1 - 指向包含“This is test \ 0”字符串的程序的只读部分的指针。

2 - memory (13 bytes) which is initialized with contents mentioned above.

2 - 用上述内容初始化的存储器(13字节)。