指针代码
时间: 2023-07-02 21:22:28 浏览: 58
以下是一个简单的指针代码示例,它展示了如何声明、初始化和使用指针变量:
```c
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("The value of x is: %d\n", x);
printf("The address of x is: %p\n", &x);
printf("The value of ptr is: %p\n", ptr);
printf("The value pointed to by ptr is: %d\n", *ptr);
return 0;
}
```
输出结果为:
```
The value of x is: 10
The address of x is: 0x7fff5fbff7a4
The value of ptr is: 0x7fff5fbff7a4
The value pointed to by ptr is: 10
```
在这个示例中,我们首先声明了一个整型变量 `x`,然后使用 `&` 运算符取得其地址,并将该地址赋值给整型指针变量 `ptr`。然后,我们分别打印出 `x` 的值、地址、`ptr` 的值和指针所指向的值。
阅读全文
相关推荐











