gpt4 book ai didi

c - 指针取消引用 balagurusamy

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:13 26 4
gpt4 key购买 nike

when 时取消引用指针有什么问题,需要时不取消引用指针。代码如下。这里有什么错误,无法理解。在 ansi c 的 balagurussamy 书中给出。

int *p,m=100 ;
p = &x ;
printf("%d",p) ;/*error*/

最佳答案

修复代码中的一些问题:

#include <stdio.h>

int main(void)
{
int *p, m = 100, x = 10;
p = &x; // In words, making 'p' to point to the address of 'x'
printf("Address %p contains a value %d..", (void *)p, *p);
// ^^ ^^^^^^^^^ ^
// Using the correct Typecasting Dereferencing
// format specifier the pointer the pointer
//
// Some code
return 0;
}

生成的输出:

Address 0x7ffdf396c128 contains a value 10..

如果您尝试相同的代码,您应该会得到不同的结果,因为地址不太可能相同。

解释:

  • x 在您提供的代码段中未定义
  • 当指针用于指向兼容的数据类型时,您可以通过取消引用指针来打印值。
  • 如果要打印指针本身的值,需要先将其转换为 void,然后使用 %p 格式说明符而不是使用 %d.
  • List of common format specifier in C programming

关于c - 指针取消引用 balagurusamy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56753146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com