3 回答

TA貢獻1111條經(jīng)驗 獲得超0個贊
char *word
char **sentence
char ***monologue
char ****biography
char *****biolibrary
char ******lol
... ...
#include <stdio.h>#include <stdlib.h>#include <string.h>int wordsinsentence(char **x) { int w = 0; while (*x) { w += 1; x++; } return w;}int wordsinmono(char ***x) { int w = 0; while (*x) { w += wordsinsentence(*x); x++; } return w;}int wordsinbio(char ****x) { int w = 0; while (*x) { w += wordsinmono(*x); x++; } return w;}int wordsinlib(char *****x) { int w = 0; while (*x) { w += wordsinbio(*x); x++; } return w;}int wordsinlol(char ******x) { int w = 0; while (*x) { w += wordsinlib(*x); x++; } return w;}int main(void) { char *word; char **sentence; char ***monologue; char ****biography; char *****biolibrary; char ******lol; //fill data structure word = malloc(4 * sizeof *word); // assume it worked strcpy(word, "foo"); sentence = malloc(4 * sizeof *sentence); // assume it worked sentence[0] = word; sentence[1] = word; sentence[2] = word; sentence[3] = NULL; monologue = malloc(4 * sizeof *monologue); // assume it worked monologue[0] = sentence; monologue[1] = sentence; monologue[2] = sentence; monologue[3] = NULL; biography = malloc(4 * sizeof *biography); // assume it worked biography[0] = monologue; biography[1] = monologue; biography[2] = monologue; biography[3] = NULL; biolibrary = malloc(4 * sizeof *biolibrary); // assume it worked biolibrary[0] = biography; biolibrary[1] = biography; biolibrary[2] = biography; biolibrary[3] = NULL; lol = malloc(4 * sizeof *lol); // assume it worked lol[0] = biolibrary; lol[1] = biolibrary; lol[2] = biolibrary; lol[3] = NULL; printf("total words in my lol: %d\n", wordsinlol(lol)); free(lol); free(biolibrary); free(biography); free(monologue); free(sentence); free(word);}
total words in my lol: 243

TA貢獻2016條經(jīng)驗 獲得超9個贊
**
void allocate(int** p){ *p = (int*)malloc(sizeof(int));}int main(){ int* p = NULL; allocate(&p); *p = 42; free(p);}

TA貢獻1982條經(jīng)驗 獲得超2個贊
假設(shè)您有一個指針,它的值是一個地址。 但現(xiàn)在你想改變那個地址。 您可以執(zhí)行pointer1=pointer2,pointer1現(xiàn)在有pointer2的地址。 但!如果您想要一個函數(shù)為您做這件事,并且您希望在函數(shù)完成后結(jié)果保持不變,那么您需要做一些額外的工作,您需要一個新的pointer3來指向pointer1,并將pointer3傳遞給該函數(shù)。 下面是一個有趣的示例(先看看輸出下面的內(nèi)容,了解一下!):
#include <stdio.h>int main(){ int c = 1; int d = 2; int e = 3; int * a = &c; int * b = &d; int * f = &e; int ** pp = &a; // pointer to pointer 'a' printf("\n a's value: %x \n", a); printf("\n b's value: %x \n", b); printf("\n f's value: %x \n", f); printf("\n can we change a?, lets see \n"); printf("\n a = b \n"); a = b; printf("\n a's value is now: %x, same as 'b'... it seems we can, but can we do it in a function? lets see... \n", a); printf("\n cant_change(a, f); \n"); cant_change(a, f); printf("\n a's value is now: %x, Doh! same as 'b'... that function tricked us. \n", a); printf("\n NOW! lets see if a pointer to a pointer solution can help us... remember that 'pp' point to 'a' \n"); printf("\n change(pp, f); \n"); change(pp, f); printf("\n a's value is now: %x, YEAH! same as 'f'... that function ROCKS!!!. \n", a); return 0;}void cant_change(int * x, int * z){ x = z; printf("\n ----> value of 'a' is: %x inside function, same as 'f', BUT will it be the same outside of this function? lets see\n", x);} void change(int ** x, int * z){ *x = z; printf("\n ----> value of 'a' is: %x inside function, same as 'f', BUT will it be the same outside of this function? lets see\n", *x);}
這是輸出:
a's value: bf94c204 b's value: bf94c208 f's value: bf94c20c can we change a?, lets see a = b a's value is now: bf94c208, same as 'b'... it seems we can, but can we do it in a function? lets see... cant_change(a, f); ----> value of 'a' is: bf94c20c inside function, same as 'f', BUT will it be the same outside of this function? lets see a's value is now: bf94c208, Doh! same as 'b'... that function tricked us. NOW! lets see if a pointer to a pointer solution can help us... remember that 'pp' point to 'a' change(pp, f); ----> value of 'a' is: bf94c20c inside function, same as 'f', BUT will it be the same outside of this function? lets see a's value is now: bf94c20c, YEAH! same as 'f'... that function ROCKS!!!.
- 3 回答
- 0 關(guān)注
- 995 瀏覽
添加回答
舉報