w*******n 发帖数: 773 | 1 main()
{
char *c1 = "abc";
char c2[] = "abc";
char *c3 = ( char* )malloc(3);
c3 = "abc";
printf("%d %d %s\n",&c1,c1,c1);
printf("%d %d %s\n",&c2,c2,c2);
printf("%d %d %s\n",&c3,c3,c3);
getchar();
}
运行结果
2293628 4199056 abc
2293624 2293624 abc
2293620 4199056 abc
看运行结果
为什么c2所指的地址,
和c2 自己的地址是一样,
我运行了,也是这样的。 | j********x 发帖数: 2330 | 2 The exceptions to this rule are when the array identifier is an
operand of either the sizeof or address-of (&) operators. When the
array is an operand of the & operator, the result is of type "pointer
to N-element array of T", or T (*a)[N], and its value is the address
of the base of the array, which is the same as the address of the
first element of the array. | h**6 发帖数: 4160 | 3 是数组的特殊性,数组和指针不多的区别之一。
试试这个:
char* c4 = c2;
printf("%d %d %s\n",&c4,c4,c4); | r*******y 发帖数: 1081 | 4 那如何才能得到存储 c2这个变量的地址?
【在 j********x 的大作中提到】 : The exceptions to this rule are when the array identifier is an : operand of either the sizeof or address-of (&) operators. When the : array is an operand of the & operator, the result is of type "pointer : to N-element array of T", or T (*a)[N], and its value is the address : of the base of the array, which is the same as the address of the : first element of the array.
| a****l 发帖数: 8211 | 5 不存在c2这个变量.
【在 r*******y 的大作中提到】 : 那如何才能得到存储 c2这个变量的地址?
| w*******n 发帖数: 773 | | g*******s 发帖数: 490 | 7 还有一个点是c1和c3指向的string literals的地址是一样的,因为是identical string。 compiler会用string pooling |
|