C指针-不同的地址 [英] C pointers - Different address

查看:102
本文介绍了C指针-不同的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习C指针,但我听不懂... 以下代码:

I'm trying to learn about C pointers but I cannot understand somethings... The following code:

#include <stdio.h>

void foo(int *x, int *y);

void foo(int *x, int *y) {
    printf("x = %p\ny = %p\n", &x, &y);
    *x = 5;
    *y = 6;
}

int main(void) {
    int a, b;
    printf("a = %p\nb = %p\n", &a, &b);
    foo(&a, &b);
    return 0;
}

为什么地址不同?第一个printf(主)输出两个地址.另一个printf(foo)输出不同的地址.我正在将地址传递给foo(&运算符).

Why are the addresses different? The first printf (main) output two addresses. the other printf (foo) output different addresses. I'm passing addresses to foo (& operator).

推荐答案

main中,您将打印xy的地址,但是在foo中,您将打印指向xy的指针.您写:

In main, you're printing addresses of x and y, but in foo, you're printing addresses of pointers to x and y. You meant to write:

printf("x = %p\ny = %p\n", x, y);

(请注意在xy之前缺少&)

(notice the lack of & before x and y)

这篇关于C指针-不同的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆