1 #include <stdio.h>
2 #include <stdlib.h>
3 //前算完算阿,以前一直以為弄懂了傳值和傳引用,但是現在//一看未必阿,特別是碰到指針的時候
4 char **data;
5 void fun(char ** pointer)
6 {
7 pointer=(char**)malloc(sizeof(char*) * 10);
8 pointer[0] = (char*)malloc(sizeof(char) * 10);
9 pointer[0][0] = 'a';
10 pointer[0][1] = 'b';
11 pointer[0][2] = '\0';
12
13 printf("IN fun: output: %s\n", pointer[0]);
14
15 }
16 int main()
17 {
18
19 fun(data);
20 printf("IN main: output: %s\n", data[0]);
21 return 0;
22 }
23
24
上面的代碼能正確執行嗎?
各位高手應該能看出來了。。
結果是:(gcc下)
IN fun: output: ab
Segmentation fault (core dumped)
愣了好一陣子,才反映過來,被傳的指針蒙騙了,
在12行加入這行就ok了。這個隨是小事,但馬虎不得阿,哈哈
data = pointer;
posted on 2008-03-28 22:54
fullfocus 閱讀(1425)
評論(9) 編輯 收藏 所屬分類:
C/C++