如何在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 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短