C++ 指针:完整指南
为了理解指针,让我们先回顾一下“常规”变量。 如果您熟悉没有指针的编程语言(如 JavaScript),那么当您听到“变量”时,您会这么想。
C++ 指针
C++ 指针是一个变量,其值是另一个变量的地址。 指针是地址的符号表示。 指针使程序能够模拟引用调用并创建和操作动态数据结构。
您必须先声明指针,然后才能使用它。 当通过标识符(或名称)声明变量时,变量与其值同义。
int number = 3; std::cout << "number is initialized with a value of " << number << "\n"; // Outputs: number is initialized with a value of 3
变量的值可以直接修改。
number++; number = number * 2; std::cout << "After modifying number, its value is now " << number << "\n"; // Outputs: After modifying number, its value is now 8
每个值都存在于内存地址中。 可以使用 & 运算符访问变量的内存地址。
std::cout << "The number variable's value lives at memory address " << &number << "\n";
运算符地址 (&)
变量的地址可以通过在变量名称前加上与号 (&) 来获得,称为地址运算符。 请参阅以下代码。
app = &var;
这会将变量 var 的地址分配给应用程序; 通过在变量 var 的名称前加上地址运算符 (&),我们不再将变量本身的内容分配给应用程序,而是分配给应用程序的地址。
在运行前无法知道变量在内存中的实际地址,但为了帮助澄清一些概念,我们假设 var 在运行时被放置在内存地址 1921 中。
什么是指针?
与标准变量相比,指针是不同的实体,因为它们存储了一些其他变量的地址或指向变量的航点。
前任。 诠释一个; //’a’ 是一个变量,它将保存一些整数值。
诠释*一个; // ‘a’ 是一个指向整数变量的指针。
一般声明
* pointerName; can be int, float, char, etc.
为什么使用 C++ 指针?
如前所述,指针保存地址,因此我们可以在函数中发送参数时使用它来降低复杂性。
假设有一个巨大的数组,我们必须将数组作为函数的参数发送,所以我们可以给出第一个变量的地址,而不是传递数组。
此外,在地址级别使用指针的计算操作比不使用指针的计算操作简单。
示例:
要知道变量的地址,我们使用引用运算符’&’,要查看存储在指针指向的地址的值,我们使用解引用运算符’*’。
int a = 10; // integer variable a int *p = &a; // referencing operator ‘&’ prefixed with integer variable a. p will now store the address of a. int b = *p; // dereferencing pointer p using ‘*’ prefixed with p. b will store the value of a that is 10.
请参阅以下 C++ 中的指针程序。
#includeusing namespace std; int main() { // Sample program to show pointers concepts. int a=20; // integer variable a int * p; // pointer to an integer p = &a; // p now holds address of a. cout << "Address of a : " << p << "\n"; int b = *p; // dereferencing using *, now b has the same value as a. cout << "Value of a: " << *p << "\n"; return 0; }
查看输出。
应用:
用于使用指针通过引用调用。
当一个变量作为引用调用中的引用传递时,在函数体内对其所做的更改也会反映在主函数中。 例如,考虑下面使用指针交易所两个数字的程序。
#includeusing namespace std; void swap_(int *x, int *y) { int temp= *x; // store the value stored at address pointed by x. *x = *y; // change value at address pointed by x to value at address y *y = temp; // change value at address pointed by y to value that was present at address of x at start. } int main() { // Sample program to show call by reference by pointers int a=20; // integer variable a int b=30; // integer variable b cout << "Before swapping : " << a << " " << b << "\n"; swap_(&a,&b); // swap function cout << "After swapping : " << a << " " << b << "\n"; return 0; }
调用交易所函数后,值在 a 和 b 之间交易所,a=30 和 b=20。 查看输出。
C++ 指针数组
C++ 中的指针数组是指存储不同变量地址的数组。
int * a[10]; // array of integer pointers
请参阅以下代码示例。
#includeusing namespace std; int main() { // Sample program to show call by reference by pointers int a[10]; int i,j; for(i=0;i<10;i++) a[i]=i+1; cout << "Without pointers :\n"; for(i=0;i<10;i++) cout << a[i] << " "; cout << "\n"; int *p[10]; // pointers to each member of array of a for(i=0;i<10;i++) p[i] = &a[i]; cout << "Using pointers :\n"; for(i=0;i<10;i++) cout << *p[i] << " "; return 0; }
查看输出。
指针和数组
数组名就是数组的起始地址。 所以,考虑int a[10]; , &a , &a[0] 表示同一件事。
要访问数组中的第 i 个索引,可以使用 *(a+i), *(i+a) , a[i] 或者我[a]. 都是一样的。
请参阅以下代码示例。
#includeusing namespace std; int main() { // Sample program to show call by reference by pointers int a[10]; int i,j; for(i=0;i<10;i++) a[i]=i+1; //All point to the same address cout << a << " " << &a << " " << &a[0] << "\n" ; // All give the same value that is the value contained at a[1] cout << *(a+1) << " " << *(1+a) << " " << a[1] << " " << 1[a] <<"\n"; return 0; }
请参阅以下输出。
指针算术
像 ++, –, +, – 这样的算术运算可以对指针变量进行运算。
考虑,int a[3] = {1,2,3};
ptr = &a[0]; ptr = ptr+1; // This will point to the next variable in the array. ptr++ will do the same thing. Ptr = ptr-1; // Again the pointer will point to the 0th index. ptr—will do the same thing.
请参阅以下代码。
#includeusing namespace std; int main() { // Sample program to show call by reference by pointers int a[3] = {1,2,3}; int * p= &a[0]; p=p+1; // point to next integer cout << "Value of 2nd element : " << *p << "\n"; p--; cout << "Value of 1st element : " << *p << "\n"; return 0; }
查看输出。
以下是一些基本类型的指针。
C++中的空指针
C++ 中的 NULL 指针不指向任何内容,并且已初始化为 NULL。 NULL 指针是 0 值。
请参阅以下代码。
#includeusing namespace std; int main() { // Sample program to show Null pointer int * p = NULL; // initialized to NULL cout << "VALUE : " << p << "\n"; return 0; }
查看输出。
C++中的空指针
void 是一种指针; 与 NULL 指针不同。 处理 void 指针时需要使用类型转换。 使用此指针,您可以有效地处理任何数据类型。 您需要包含该数据类型。
请参阅以下程序。
#includeusing namespace std; int main() { // Sample program to show Void pointer int a=10; void * p; // initialized to NULL p = &a; // address of a cout << "VALUE : " << *((int*)p) << "\n"; // typecasting used otherwise there will be error. return 0; }
查看输出。
悬空指针
指向不再存在的内存位置的指针,即该内存位置已被删除。 请参阅以下程序。
#includeusing namespace std; int main() { // Sample program to show dangling pointer int *a = new int[2]; a[0] = 10; a[1] = 20; int *p = &a[0]; delete a; // comment this line to avoid dangling pointer cout <<"Value is: "<<*p<<"\n"; // Garbage value will be printed return 0; }
请参阅以下输出。
C++ 指向指针的指针
存储另一个指针地址的指针称为指向指针的指针。 这里,在这种情况下,一个指针存储另一个指针的地址,然后第二个指针存储一个变量的地址。
下面的程序告诉如何处理它。
#includeusing namespace std; int main() { // Sample program to show pointer to pointer int a = 10; int *p = &a; // stores the address of integer a int **q = &p; // stores the address of pointer p cout << "Address of a : " << p << "\n"; cout << "Address of p : " << q << "\n"; cout << "Value of p : " << p << " " << *q << "\n" ; // All are same cout << "Value of a : " << a << " " << *p << " " << **q << "\n"; // All are same return 0; }
查看输出。
而已。
帖子 C++ 指针:完整指南首先出现在 AppDividend 上。