指针用于存储变量的地址。要在C / C ++中声明指针变量,在其名称前使用星号(*)。
*pointer_name
#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
#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