[問題] pointer's pointer

看板C_and_CPP (C/C++)作者 (satesqure)時間13年前 (2013/01/26 01:42), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Platform: Code:block Complier: gcc 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 利用指標的指標(ptrR2)動態配置了一個3 by 2的矩陣, 想驗證的在資料型態為Dobule的情況下, 1.指標的加減是否滿足*ptrR2+1會比*ptrR2多了8個byte. ,但結果與預期不同,癥結點在哪 餵入的資料(Input): 預期的正確結果(Expected Output): *ptrR2+1 should be bigger than *PtrR2 by 8 bytes. 錯誤結果(Wrong Output): ptrR2[0][0]=0(0x804a0a8) ptrR2[1][0]=1(0x804a0e8) ptrR2[2][0]=2(0x804a128) ptrR2[0][1]=3(0x804a0ac) ptrR2[1][1]=4(0x804a0ec) ptrR2[2][1]=5(0x804a12c) ptrR2[0][2]=6(0x804a0b0) ptrR2[1][2]=7(0x804a0f0) ptrR2[2][2]=8(0x804a130) ptrR2[0][3]=9(0x804a0b4) ptrR2[1][3]=10(0x804a0f4) ptrR2[2][3]=11(0x804a134) 0x804a0a8->this is *ptrR2 0x804a0ac->this is (*ptrR2)+1 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define row 3 #define column 4 int main(void) { int i; int j; int sum=0; int **ptrR2; ptrR2=(double**)malloc(sizeof(double*)*row); for(i=0;i<row;++i) { ptrR2[i]=(double*)malloc(sizeof(double)*column); } for(j=0;j<column;++j) { for(i=0;i<row;++i) { *(*(ptrR2+i)+j)=sum++; } } for(j=0;j<column;++j) { for(i=0;i<row;++i) { printf("ptrR2[%d][%d]=%d(%p)\n",i,j,ptrR2[i][j],&ptrR2[i][j]); } } printf("%p\n",*ptrR2); printf("%p",(*ptrR2)+1); return 0; } 補充說明(Supplement): This is the first time I post on this board.I don't know how to highlight crux. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.168.248.129

01/26 01:59, , 1F
int **ptrR2; 改成 double **ptrR2;
01/26 01:59, 1F
文章代碼(AID): #1H0iGWAu (C_and_CPP)
文章代碼(AID): #1H0iGWAu (C_and_CPP)