如何在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 教师闺蜜祝福语简短