繁体   English   中英

指向字符数组输出的指针

[英]Pointer to character array output

当我们声明一个指向任何整数的指针时,说我们声明它像

int i=5;
int *p=&i;

我们使用*p来获取值 5,如果我们想要地址,我们使用不带星号的p来获取地址。 但是在字符数组的情况下,当我们说

char *str="HELLO";

我们只是使用str来获取"HELLO"作为 printf 函数的输出。 像这样,

printf("%s",str);

这里我们使用了不带星号的str

当我们使用不带星号的指针变量时,为什么我们没有获得地址,而是在此处获得“HELLO”作为输出,而在指向整数的指针的情况下获得地址?

好的,首先int *p=5; 是错的! 你不能在指针的声明中做到这一点。 那时指针需要一个地址。 你应该做int *p; 然后*p = 5; 为其赋值。

printf("%s",str); // prints the value of the variable, because its a pointer.
printf("%s",*str); // the * will cause to a segmentation fault

int *p // the * there is just used to tell the compiler that you are declaring a pointer of type integer.

/* another scope after declaring your pointer */

*p //is used to get value of a pointer variable.(dereference it)
&p //is used to get the adress of a pointer variable.
p //contains an adress.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM