为什么要用双重间接?或者为什么使用指针指向指针?在C中什么时候应该使用双重间接?有人能举个例子来解释吗?我所知道的是,双重间接方向是指向指针的指针。为什么我需要一个指向指针的指针?
3 回答
catspeake
TA贡献1111条经验 获得超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条经验 获得超9个赞
**
void allocate(int** p){
*p = (int*)malloc(sizeof(int));}int main(){
int* p = NULL;
allocate(&p);
*p = 42;
free(p);}
临摹微笑
TA贡献1982条经验 获得超2个赞
假设您有一个指针,它的值是一个地址。 但现在你想改变那个地址。 您可以执行pointer1=pointer2,pointer1现在有pointer2的地址。 但!如果您想要一个函数为您做这件事,并且您希望在函数完成后结果保持不变,那么您需要做一些额外的工作,您需要一个新的pointer3来指向pointer1,并将pointer3传递给该函数。 下面是一个有趣的示例(先看看输出下面的内容,了解一下!):
#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 关注
- 1056 浏览
添加回答
举报
0/150
提交
取消
