c - C中指针指向指针的问题

标签 c

这就是我想要实现的目标。 在指针“r”内部,我有指针“p”的地址,现在我尝试通过调用函数 moveforward() 使用指针算术移动“p”的值(这是一个地址)。 我收到此错误:

void value not ignored as it ought to be
movingresult=moveforward(r);

这里出了什么问题?虽然处理指针到指针有点复杂。

#include <stdio.h>

void moveforward(int** y) {
     *y = *y+1;
     printf("value of *y in function : %p\n",*y);

}


int main () {
int a = 16;
int *p;
int **r;
p = &a;
r = &p;
int **movingresult;

printf("Value of p before moving :%p\n",p);
movingresult=moveforward(r);
printf("Value of p after moving :%p",movingresult);

return 0;

}

我刚刚更改了代码,所以现在看起来像这样,一切运行良好,但结果不是我所期望的。

#include <stdio.h>

void moveforward(int** y) {
 *y = *y+1;
 printf("Value of *y now has been moved to : %p\n",*y);

}


int main () {
int a = 16;
int *p;
int **r;
p = &a;
r = &p;
int **movingresult;

printf("Value of p before moving :%p\n",p);
moveforward(r);
printf("Value of p after moving :%p\n",r);

return 0;

}

OUTPUT :
Value of p before moving :0x7fffa5a1c184
Value of *y now after moving : 0x7fffa5a1c188
Value of p after moving :0x7fffa5a1c178

我的期望:移动后的“p”必须等于 *y,即 0x7fffa5a1c188

最佳答案

当调用函数moveforward时,r的指针地址不会改变。 前进(r); 调用 moveforward 后,r 的地址还没有改变。 所以如果你执行 printf("移动后p的值:%p\n",r);

r的地址仍然没有改变。

如果你想得到你的期望值,你应该这样做: printf("移动后p的值:%p\n",*r);

关于c - C中指针指向指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941122/

相关文章:

c - 如何修复终端中的段错误?

C : multiple lines of input based on previous inputs

c - 我的函数没有返回任何内容,但可以编译并且看起来是正确的

c - free() 不释放内存?

c - 将 int 添加到多维 char 数组

c - gcc 信息页面中的 "-std=standard"是什么意思?

c++ - Makefile 中的这一行是什么意思?

c - Visual Studio 2013 不会编译 C 数组声明

c - 格式字符串利用长度

c - 在 STM32L1 上删除我的闪存