code.club

 找回密碼
 立即註冊
搜索
查看: 6441|回復: 3

指標的一些心得和筆記

[複製鏈接]
發表於 2015-1-22 23:34:55 | 顯示全部樓層 |閱讀模式
本帖最後由 enter 於 2015-1-24 21:27 編輯

char str[80];
str[80]="hello world";
str="hello world";

後來再賦值都是不可以的。

要就一開始就給值如:
char str[]="hello world";
char str[4]={'a','b','c'};//這如果不用' ' 則系統會把a b c當成函數中的變數而出錯誤訊息。

不然就是用指標,如:
char *p;
p="hello world";
printf(p);
這才可以。

數字也一樣。
int x[10];
x[10]={1,2,3,4,5,6,7,8,9,10};
也不可以。
必須要:
int x[10]={1,2,3,4,5,6,7,8,9,10];才可以。
而且 int *x;
x={1,2,3,4,5};也不可以,這點和char *p不同。

只有單個字母和數字可以事後再賦值,例如:

int x;
x=5;

char ch;
ch='a';

這二者都可以印出來。
回復

使用道具 舉報

 樓主| 發表於 2015-1-23 22:17:05 | 顯示全部樓層
#include <stdio.h>

int main(void)
{
int num[2][3][3]=
{
10,20,30, 40,50,60, 70,80,90,
11,22,33, 44,55,66, 77,88,99
};
int *p=&num;
int **q=&p;
int ***r=&q;
printf("%d %d %d\n",num[1][2][1],*(*(*(num+1)+2)+1),*(*(num[1]+2)+1)); //88, 88, 88
printf("%d\n",*(p+12)); //44
printf("%d %d %d\n",***r,**q,*p);//10, 10, 10
printf("%d %d %d\n",*(p+2),*(*q+2),*(**r+2));// 30, 30, 30 -->把它都當是*(p+2)所以都只留一個*在括號之外。


return 0;
}
回復 支持 反對

使用道具 舉報

 樓主| 發表於 2015-1-24 01:06:46 | 顯示全部樓層
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x[2][3][4]={
0,10,20,30,  40,50,60,70,  80,90,100,110,
120,130,140,150,  160,170,180,190,  200,210,220,230};

char *str[]={"how","old","are","you"};
char *p;
p=str;

printf("%d \n",  *(**x+1) );//10,*愈多就愈往小陣列靠
printf("%d \n",  **(*x+1) );//40
printf("%d \n",  *(*(*x+1)+0) );//40 請將這種視為+0,才能比較詳細看出內涵
printf("%d \n",  ***(x+1) );//120
printf("%d\n",   *(*(*(x+1) +1)  +2) );//180
printf("%d\n",**(*x+5));//其實這都可以視為一維陣列
printf("%d\n", *(*(*x+2)+2)     );//100
printf("%c %c %c %c %c\n",str[2][2],*(*(str+2)+2),*(str[2]+2),*(p+8)); // r, 但*(str+8)是錯的
printf("%s %s %s %s\n",str,p,str[0]);// p[0]是錯的
printf("%s %s %s\n",str+2,p+2,str[0]+2); // str+2是指從第三個次陣列開始,p+2和str[0]+2則都從第三個元素開始。
printf("%d %d\n",***x+1,***(x+1));
printf("%s \n%s\n",*(str+1),*str+1);
printf("%p %p %p %p\n",&x[0][0][0],x,*x,**x); //四個都是得出x陣列第一元素的位址。
printf("%p %p\n",&x[1][1][2],*(*(x+1)+1)+2);


return 0;
}
回復 支持 反對

使用道具 舉報

 樓主| 發表於 2015-1-24 21:27:22 | 顯示全部樓層
#include <stdio.h>

int main(void)
{
//char *str[]={"this is a book!","good morning.","how are you?","you're welcome!"};
char str[][30]={"This is a book!","Good morning.","How are you?","You're welcome!"};
char *p,**q,***r;
//p=*str;//前面是*str[]的型態,則這裡要寫成p=*str。
p=str;//要前面是str[][]的型態,這裡才能直接p=str。
q=&p;
r=&q;

printf("%s\n%s\n",*str,p);//*str適合先前面 *str[]和str[][]二種型式。
printf("%s\n%s\n",str,p);//如果先前是寫成*str[],則這裡就一定要寫成*str才可以,只有str是不對的。
printf("%s\n%s\n",*q,**r);
printf("%c %c\n",str[2][4],*(*(str+2)+4));
printf("%c %c %c\n",*(p+1),*(*q+1),*(**r+1));//固定一個*在()外面。
printf("%c %c %c\n",*(p+64),*(*q+64),*(**r+64));//用指標則只能從第一元素往後算。
printf("%s\n%s\n%s\n",p+2,*q+2,**r+2);//這只是從第一次陣列的第三元素起算,不是從第三次陣列起算。
printf("%s\n%s\n",str+2,*(str+2));
printf("%s\n%s\n",*str+2,*(str+0)+2);
//printf("%c %c\n",**str+2,**(str+2));//**str就是第一元素,加2是指該字元再加2。str+2就表示指到第三次陣列,而**(str+2)就是第三陣列的第一元素。
return 0;
}
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-29 16:17 , Processed in 0.086537 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表