網域名稱俱樂部

網域名稱俱樂部 (https://www.domainclub.org/index.php)
-   一般軟體與網路使用討論 (https://www.domainclub.org/forumdisplay.php?f=43)
-   -   指標問題之一 (https://www.domainclub.org/showthread.php?t=36718)

哈啦 2014-06-10 03:05 PM

指標問題之一
 
習題有此程式:
#include <stdio.h>

int main(void)
{
char str[]="hello world!";
char *p;
int x;

p=str;
for(x=0;p[x];x++)
printf("%c",p[x]);

return 0;
}

請問為何p=str而不是p=&str?
我有試過p=&str,結果跑出來的答案也是一樣的,但為何課本上不加&?二者所代表的意義應不太相同吧?雖然本題的結果是一樣的?

thanks.

哈啦 2014-06-10 03:44 PM

我有一個相關的心得如下題:
#include <stdio.h>

int main(void)
{
char str[80],*p;
printf("enter:\n");
gets(str);
p=&str;
printf("%c %s",*p,p);

return 0;
}

一是,p=str 或者 p=&str好像結果是一樣的?不知是否隱含了什麼不可知的差異?

二是,如果*p則只能印出來這個p所指到的str字串的第一個字符,所以要用%c,如果想印出整列字串%s,則p就可以了,用*p反而出錯。

但我只知其然而不知其所以然:whoknows。還望先進們解釋指導一下。:bow 我覺得如果不了解一下實體記憶體的長相和配置,似乎就會紙上談兵,一知半解,是嗎?


所有時間均為 +8。現在的時間是 12:17 AM

Powered by vBulletin® 版本 3.8.4
版權所有 ©2000 - 2024,Jelsoft Enterprises Ltd.