[英]Passing char pointers in C
我正在学习C中的指针,所以我这样做是为了查看我在做某事时应该希望的输出。 这里是:
int characters(char temp[])
{
printf("\nAddress of string by &temp: %p",&temp);
printf("\nAddress of string by temp: %p",temp);
printf("\nContent of string: %s",temp);
return 0;
}
int main()
{
char s[] = "Prafulla Shahi";
char *t=s;
clrscr();
characters(t);
printf("\nMain\nAddress of string by &s: %p",&s);
printf("\nAddress of string by s: %p",s);
printf("\nContent of string: %s",s);
printf("\nAddress of pointer by &t: %p",&t);
printf("\nAddress of pointer by t: %p",t);
printf("\nContent of pointer: %s",t);
getch();
return(0);
}
我得到的输出是这样的:
Address of string by &temp: 0x7fff4ab45788
Address of string by temp: 0x7fff4ab457b0
Content of string: Prafulla Shahi
Main
Address of string by &s: 0x7fff4ab457b0
Address of string by s: 0x7fff4ab457b0
Content of string: Prafulla Shahi
Address of pointer by &t: 0x7fff4ab457a8
Address of pointer by t: 0x7fff4ab457b0
Content of pointer: Prafulla Shahi
我的问题是:当温度和温度问题时,为什么临时数组的地址显示两个不同的值?
我的理解是,temp是一个数组变量,但它有自己的地址(与指针t类似的行为。为什么它的行为不像通常的变量数组?)。 main()中的数组s []也是一个数组,但是当由&s和s调用时,它的地址显示相同的值。
有人可以解释一下吗?
char s[] = "Hello world";
s
是一个数组对象(用字符串文字初始化)。 数组的地址和数组的第一个元素的地址是相同的(只有类型不同)。
所以:
(char *) &s == s /* value of the == expression is 1 */
但:
char *p = "Hello world";
p
是指针对象(指向字符串文字)。 p
的地址是指针对象的地址,该地址不同于p
的值, p
是指向"Hello world"
字符串的第一个字符的指针。
所以:
(char *) &p == p /* value of the == expression is 0 */
为了完整回到您的示例,我还必须补充说,函数声明中的数组参数在C中调整为指针类型的参数。 所以这两个函数声明是等价的:
int characters(char temp[]) { /* ... */ }
和
int characters(char *temp) { /* ... */ }
这也意味着内部characters
功能,我们有:
(char *) &temp == temp /* value of the == expression is 0 */
因为temp
是指针对象而不是数组对象。
我的理解是,
temp
是一个数组变量
不幸的是, 不,它不是。
将数组传递给函数时,它会衰减为指向其第一个元素的指针。 作为一个非常合乎逻辑的结果,决定当你像数组一样声明一个函数参数时,它将被解释为一个指针。 因此,在函数参数声明中, 并且仅在函数参数声明中,数组表示法与指针限定符等效。 所以
int characters(char temp[])
是一样的
int characters(char *temp)
指针是一个保存值(地址)并且也有自己的地址的对象,因此当然temp
与&temp
。
你有没有在函数参数外面声明temp
作为一个真正的数组 (而不是一个指针),你的期望是否已经满足; 然后当然&temp
会指向与指针temp
本身衰减相同的地址。
传递给函数时,数组被分解为指针(指向其第一个元素)。 您可以使用数组表示法来简化您的生活,但就编译器而言,temp是char *,而不是实际的数组。 这就是为什么它的行为与t相同,而不是s。
s
是静态分配的数组。 因此,它的值和第一个元素的地址是相同的。
t
和temp
都是指向s
新指针变量。 它们的价值是s
的地址。 但是这些变量中的每一个都是单独分配的,因此每个变量都有自己的地址。
s (address = 0x7fff4ab457b0)
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| P | r | a | f | u | l | l | a | | S | h | a | h | i |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
^
| temp (address = 0x7fff4ab45788)
| +----------------+
+--| 0x7fff4ab457b0 |
| +----------------+
|
| t (address = 0x7fff4ab457a8)
| +----------------+
+--| 0x7fff4ab457b0 |
+----------------+
在你的character(char temp[])
定义函数中, temp
是指向char
的指针。 当您将数组的名称作为参数传递时,它的第一个元素的地址将被复制到temp
。 检查一下: http : //www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm
因此,当您尝试打印temp
的值时,它会打印数组的第一个元素的地址。 但是,当您尝试打印&temp
的值时&temp
您不打印数组的地址,而是打印指针本身的地址。
要以其他方式打印地址,您可以使用此方法:
printf("\nAddress of string by &temp: %u",&temp[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.