nam*_*run 42 c pointers memory-address
当我运行此代码.
#include <stdio.h>
void moo(int a, int *b);
int main()
{
    int x;
    int *y;
    x = 1;
    y = &x;
    printf("Address of x = %d, value of x = %d\n", &x, x);
    printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
    moo(9, y);
}
void moo(int a, int *b)
{
    printf("Address of a = %d, value of a = %d\n", &a, a);
    printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}
我一直在编译器中收到此错误.
/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
你可以帮帮我吗?
谢谢
blargman
Car*_*rum 85
您想用来%p打印指针.从规格:
p参数应该是指针void.指针的值以实现定义的方式转换为打印字符序列.
并且不要忘记演员表,例如
printf("%p\n",(void*)&a);
小智 6
当您打算打印任何变量或指针的内存地址时,使用%d将不会执行该作业并将导致一些编译错误,因为您尝试打印出一个数字而不是一个地址,即使它确实有效,你有一个意图错误,因为内存地址不是数字.价值0xbfc0d878肯定不是数字,而是地址.
你应该使用的是什么%p.例如,
#include<stdio.h>
int main(void) {
    int a;
    a = 5;
    printf("The memory address of a is: %p\n", (void*) &a);
    return 0;
}
祝好运!
| 归档时间: | 
 | 
| 查看次数: | 141739 次 | 
| 最近记录: |