如何在C / C ++中声明指针变量?
指针用于存储变量的地址。要在C/C++中声明指针变量,在其名称前使用星号(*)。
声明
*pointer_name
在C中
示例
#include <stdio.h>
int main() {
//普通整数变量
int a = 7;
//一个指针变量,保存a的地址。
int *p = &a;
// Value stored is value of variable "a"
printf("Value of Variable : %d\n", *p);
//it will print the address of the variable "a"
printf("Address of Variable : %p\n", p);
//重新分配值。
*p = 6;
printf("Value of the variable is now: %d\n", *p);
return 0;
}输出结果
Value of Variable : 7 Address of Variable : 0x6ffe34 Value of the variable is now: 6
在C++中
示例
#include <iostream>
using namespace std;
int main() {
//普通整数变量
int a = 7;
//一个指针变量,保存a的地址。
int *p = &a;
// Value stored is value of variable "a"
cout<<"Value of Variable : "<<*p<<endl;
//it will print the address of the variable "a"
cout<<"Address of Variable : "<<p<<endl;
//重新分配值。
*p = 6;
cout<<"Value of the variable is now: "<<*p<<endl;
return 0;
}输出结果
Value of Variable : 7 Address of Variable : 0x6ffe34 Value of the variable is now: 6
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短