我应该使用哪种格式说明符来打印变量的地址?下面很多我很困惑.
%u - 无符号整数
%x - 十六进制值
%p - 无效指针
哪个是打印地址的最佳格式?
#include<stdio.h>
#include<string.h>
int main()
{
char * p = "abc";
char * p1 = "abc";
printf("%d %d", p, p1);
}
Run Code Online (Sandbox Code Playgroud)
当我打印两个指针的值时,它打印相同的地址.为什么?
考虑下面的 C 代码。我原以为变量bar每次都会被实例化,因此会指向内存中的不同地址,但事实并非如此。
for (i = 2; i < 7; i++) {
struct foo bar;
printf("struct %u\n", bar);
}
Run Code Online (Sandbox Code Playgroud)
输出:
struct 13205520
struct 13205520
struct 13205520
struct 13205520
struct 13205520
Run Code Online (Sandbox Code Playgroud)
如果不明显,我想要的是在 5 个不同的位置生成 5 个不同struct的 s——嗯,实际上是 5 个不同的指向structs 的指针。我怎样才能做到这一点?
以下C语句中的标记数.
printf("i = %d, &i = %x", i, &i);
Run Code Online (Sandbox Code Playgroud)
我想这里有12个令牌.但我的回答是错误的.
谁能告诉我如何在上述C语句中找到令牌?
PS:我知道令牌是源程序文本,编译器不会将其分解为组件元素.
#include <stdio.h>
int main(void)
{
int i = 3;
int* j = &i;
printf("%u",j);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码应该打印出包含整数3的内存块的地址(一个无符号整数)。但我却收到了这个错误
error: format specifies type 'unsigned int' but the argument has type 'int *'- 。
我从各种来源确认:
1.*j指“存储在 j 中的地址处的值”
2.&j指存储指针 j 的内存块的地址。
3. j 包含一个 unsigned int 值,它是 j 指向的内存块的地址。
我尝试使用指针来玩一些指定值'i'和我发现的内容,因为有两个不同的地址分配给声明%u和%lu,%llu.变量如何可能在同一个执行实例中具有两个不同的地址 -
#include <stdio.h>
int main(void)
{
int i;
float f;
printf("\nEnter an integer:\t");
scanf("%d",&i);
printf("\nValue of address of i=%u",&i);
printf("\nvalue of address of i=%d",&i);
printf("\nValue of address of i=%lu",&i);
printf("\nValue of address of i=%llu",&i);
printf("\nvalue of i=%d",i);
printf("\nvalue of i=%u",i);
printf("\nvalue of i=%lu",i);
printf("\nvalue of i=%llu\n",i);
}
Run Code Online (Sandbox Code Playgroud)
这是输出 -
aalpanigrahi@aalpanigrahi-HP-Pavilion-g4-Notebook-PC:~/Desktop/Daily programs/pointers$ ./pointer001
Enter an integer: 12
Value of address of i=1193639268
value of address of i=1193639268
Value of address of i=140725797092708
Value of address of i=140725797092708
value of i=12 …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(void)
{
int *ptr;
printf("The Hex value of ptr is 0x%x",ptr);
printf("The pointer value of ptr is %p",ptr);
}
Run Code Online (Sandbox Code Playgroud)
和输出有些不同,我不知道为什么
The Hex value of ptr is 0x24a77950
The pointer value of ptr is 0x7fff24a77950
Run Code Online (Sandbox Code Playgroud)
它显示ptr的值是一个十六进制整数,但是十六进制输出缺少part 7fff。
这是printf格式问题还是其他?