C指標(1) 指標在陣列中的應用(程式講解)

2022-05-17 20:59:53 字數 2324 閱讀 6225

#include int main()

結果:

str:china beijing fujian

pstr指向str[6]:b

#include int main(void)

結果:

str:china beijing fujian

pstr->str:china beijing fujian

&str=0x7fffd12e1c80

pstr=0x7fffd12e1c80

2-3

#include int main(void)

return 0;

}

結果:

c:0x7fff13c1f1b0

h:0x7fff13c1f1b1

i:0x7fff13c1f1b2

n:0x7fff13c1f1b3

a:0x7fff13c1f1b4

:0x7fff13c1f1b5

f:0x7fff13c1f1b6

u:0x7fff13c1f1b7

j:0x7fff13c1f1b8

i:0x7fff13c1f1b9

a:0x7fff13c1f1ba

n:0x7fff13c1f1bb

#include #define weeknum 7               //定義乙個巨集

int main(void)

; printf("please input today is:");

scanf("%d",&temp);

if(temp<=weeknum)

else

return 0;

}

結果:

please input today is:4

tomorrow i

2-5

#include #define len 8               

int main(void)

; char idx,*pstr;

pstr=str;

printf("please input (0-9)and enter:\n");

scanf("%d",&idx);

if(idx結果:

please input (0-9)and enter:

4the character is:e

#include #define len 15

int main(void)

; //定義陣列str並初始化

printf("str[0]:%s\n",str[0]); //%s一次性輸出乙個字串,若要一次性輸出乙個字串需要知道字串的首位址,str[0]即為首位址

printf("str[1]:%s\n",str[1]);

return 0;

}

結果:

str[0]:fujian

str[1]:huian

2-10

#includeint main()

; printf("please input number:\n");

scanf("%d",&temp);

if(temp<7)

}return 0;

}

結果:

please input number:

1monday

#include int main(void)

結果:

fujian 2018

#include int main(void)

結果:

fujian 2018

利用陣列形式定義字串就需要知道字串大小,而指標形式則沒有這個限制

2-13  

#include int main()

return 0;

}

結果:

fujian 2018

0x4006c8

f 0x4006c8

u 0x4006c9

j 0x4006ca

i 0x4006cb

a 0x4006cc

n 0x4006cd

0x4006ce

2 0x4006cf

0 0x4006d0

1 0x4006d1

8 0x4006d2

一步乙個腳印...... 

指標在陣列中的應用 !

乙個變數有位址,乙個陣列包含若干個元素,每個陣列元素都在記憶體中占用儲存單元,他們都有響應的位址。既然指標可以指向變數,也就意味著它也可以指向陣列元素,即把某個元素的位址放到乙個指標變數中。所以所謂陣列元素的指標就是陣列元素的位址。定義乙個有10個元素的整形陣列,int a 10 定義乙個指向整形變...

C語言指標(2) 指標的應用

include include void swap int x,int y intmain 執行結果 交換前a 1,b 2 交換後a 2,b 1 請按任意鍵繼續.可以用乙個指標變數指向乙個陣列元素。例如 int a 10 int p a 0 當然定義時也可寫成 int p a include inc...

C 面試 (1) 指標

指標是c 中一類頗具特色的資料型別,允許直接操作記憶體位址,實現記憶體的動態分配。指標問題通常包括指標常量,常量指標,陣列指標,指標陣列,函式指標,指標傳值等。指標和引用的區別 非空區別。在任何情況下都不能使用指向空值的引用。因此如果你使用乙個變數並讓它指向乙個物件,但是該變數在某些時候也可能不指向...