C Pointer to Pointer and Pass by Reference

I'm trying to learn C, and I'm hung up a bit on Pointers to Pointers. I think I understand why you need them, but can't quite get my head wrapped around what is happening. For instance, the following code doesn't seem to work as I would expect it to: #include int newp(char **p) { char d = 'b'; *p = &d; /*printf("**p = %c\n", **p);*/ return 1; } int main() { char c = 'a'; char *p = &c; int result; result = newp(&p); printf("result = %d\n", result); printf("*p = %c\n", *p); printf("c = %c\n", c); return 0; } The result I get is this: result = 1 *p = c = a *p prints as nothing. Instead, I would expect `*p = b`. **However**, if I uncomment line 6 (the `printf` in the `newp` function), then I get this: **p = b result = 1 *p = b c = a What am I missing?

以上就是C Pointer to Pointer and Pass by Reference的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准