指针是存储另一个变量的地址的变量。与保存某种类型值的其他变量不同,指针保存变量的地址。例如,整数变量保存(或者可以说是存储)整数值,但整数指针保存整数变量的地址。在本指南中,我们将在示例的帮助下讨论 C 编程中的指针。
在我们讨论 C 中的指针之前,让我们举一个简单的例子来理解变量地址的含义。
一个简单的例子,了解如何在没有指针的情况下访问变量的地址?
在这个程序中,我们有一个int类型的变量。num的值是 10,这个值必须存储在内存中的某个地方,对吧?为保存该变量值的变量分配一个内存空间,该内存空间有一个地址。例如,我们住在一所房子里,我们的房子有一个地址,帮助其他人找到我们的房子。同样,变量的值存储在内存地址中,这有助于 C 程序在需要时找到该值。
因此,假设分配给变量num的地址是0x7fff5694dc58,这意味着我们应该将赋给num的任何值存储在以下位置:0x7fff5694dc58。见下图。
#include <stdio.h>int main(){int num = 10;printf("Value of variable num is: %d", num);/* To print the address of a variable we use %p* format specifier and ampersand (&) sign just* before the variable name like &num.*/printf("\nAddress of variable num is: %p", &num);return 0;}
输出:
Value of variable num is: 10Address of variable num is: 0x7fff5694dc58

C 中指针的一个简单例子
该程序显示如何声明和使用指针。我们可以使用指针做其他一些事情,我们在本指南后面讨论过它们。现在,我们只需要知道如何将指针链接到变量的地址。
需要注意的重点是:指针的数据类型和变量必须匹配,
int指针可以保存int变量的地址,类似地用float数据类型声明的指针可以保存float变量的地址。在下面的示例中,指针和变量都是int类型。
#include <stdio.h>int main(){//Variable declarationint num = 10;//Pointer declarationint *p;//Assigning address of num to the pointer pp = #printf("Address of variable num is: %p", p);return 0;}
输出:
Address of variable num is: 0x7fff5694dc58
C 指针 - 与指针一起使用的运算符
让我们讨论一下用于 C 中的指针的运算符。
&地址运算符
我们在第一个例子中已经看到,我们可以使用&符号显示变量的地址。我用&num来访问变量num的地址。&运算符也称为地址运算符。
printf("Address of var is: %p", &num);
注意事项:%p是一种格式说明符,用于以十六进制格式显示地址。
既然您知道如何获取变量的地址,但如何将该地址存储在其他变量中? 这就是指针有用的地方。如本指南开头所述,C 编程中的指针用于保存另一个变量的地址。
指针就像另一个变量,主要区别在于它存储另一个变量的地址而不是值。
“地址值”(*)运算符
*运算符也称为地址值运算符。
如何声明指针?
int *p1 /*Pointer to an integer variable*/double *p2 /*Pointer to a variable of data type double*/char *p3 /*Pointer to a character variable*/float *p4 /*pointer to a float variable*/
以上是指针声明的几个例子。 如果你需要一个指针来存储整数变量的地址,那么指针的数据类型应该是 int 。同样的情况与其他数据类型有关。
通过使用*运算符,我们可以通过指针访问变量的值。
例如:
double a = 10;double *p;p = &a;
*p会给我们变量a的值。以下语句将显示 10 作为输出。
printf("%d", *p);
同样,如果我们像这样为*指针赋值:
*p = 200;
它会改变变量a的值。上面的语句会将a的值从 10 更改为 200。
使用&和*的指针示例
#include <stdio.h>int main(){/* Pointer of integer type, this can hold the* address of a integer type variable.*/int *p;int var = 10;/* Assigning the address of variable var to the pointer* p. The p can hold the address of var because var is* an integer type variable.*/p= &var;printf("Value of variable var is: %d", var);printf("\nValue of variable var is: %d", *p);printf("\nAddress of variable var is: %p", &var);printf("\nAddress of variable var is: %p", p);printf("\nAddress of pointer p is: %p", &p);return 0;}
输出:
Value of variable var is: 10Value of variable var is: 10Address of variable var is: 0x7fff5ed98c4cAddress of variable var is: 0x7fff5ed98c4cAddress of pointer p is: 0x7fff5ed98c50

让我们举更多的例子来更好地理解它,让我们说我们有一个char变量ch和一个指向它的指针ptr。
char ch='a';char *ptr;
读取ch 的值
printf("Value of ch: %c", ch);orprintf("Value of ch: %c", *ptr);
改变ch 的值
ch = 'b';or*ptr = 'b';
上面的代码会将值'a'替换为'b'。
您能猜出以下 C 程序的输出吗?
#include <stdio.h>int main(){int var =10;int *p;p= &var;printf ( "Address of var is: %p", &var);printf ( "\nAddress of var is: %p", p);printf ( "\nValue of var is: %d", var);printf ( "\nValue of var is: %d", *p);printf ( "\nValue of var is: %d", *( &var));/* Note I have used %p for p's value as it represents an address*/printf( "\nValue of pointer p is: %p", p);printf ( "\nAddress of pointer p is: %p", &p);return 0;}
输出:
Address of var is: 0x7fff5d027c58Address of var is: 0x7fff5d027c58Value of var is: 10Value of var is: 10Value of var is: 10Value of pointer p is: 0x7fff5d027c58Address of pointer p is: 0x7fff5d027c50
关于指针的更多主题
1) 指向指针的指针 - 一个指针可以指向另一个指针(这意味着它可以存储另一个指针的地址),这样的指针称为双重指针或者指针的指针。
2) 将指针传递给函数 - 指针也可以作为参数传递给函数,使用此功能可以按引用调用函数,并且可以将数组传递给函数。
3) 函数指针 - 函数指针就像另一个指针,它用于存储函数的地址。函数指针也可用于调用 C 程序中的函数。
