Menu Close

C语言 – 指针声明和初始化

就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明。 然后在程序中才能使用它们,指针需要符合C的命名规则。

指针声明具有以下形式:

data_type * pointer_variable_name;
  • data_type 是指针的基本类型,它必须是一个有效的 C 数据类型,反映了指向的变量的数据类型;
  • pointer_variable_name 是指针变量的名称;
  • * – 用来声明指针的星号 (间接寻址运算符)* 与乘法中使用的星号是相同的。但是,在这个语句中,星号是用来指定一个变量是指针。

例如:

int *ip; /* 一个整型的指针 */
double *dp; /* 一个 double 型的指针 */
float *fp; /* 一个浮点型的指针 */
char *ch; /* 一个字符型的指针 */

所有实际数据类型,不管是整型、浮点型、字符型,还是其他的数据类型,对应指针的类型都是一样的,都是一个代表内存地址的十六进制数。

不同数据类型的指针之间唯一的不同是,指针所指向的变量或常量的数据类型不同。

指针变量同普通变量一样,使用之前不仅要定义说明,而且必须赋予具体的值。未经赋值的指针变量不能使用,否则将造成系统混乱,甚至死机。指针变量的赋值只能赋予地址,决不能赋予任何其它数据,否则将引起错误。在C语言中,变量的地址是由编译系统分配的,对用户完全透明,用户不知道变量的具体地址。

声明指针之后,我们像使用变量地址的标准变量一样对其进行初始化。 如果指针未在程序中初始化和使用,则结果将不可预测,并且可能会造成灾难性的后果。

要获取变量的地址,我们使用连字号(&)运算符,并在需要其地址的变量名称之前放置。

pointer = &variable;

例1.指针和变量的关系

#include <stdio.h>
int main()
{
   int a=10;    //variable declaration
   int *p;      //pointer variable declaration
   p=&a;        //store address of variable a in pointer p
   printf("Address stored in a variable p is:%x\n",p);  //accessing the address
   printf("Value stored in a variable p is:%d\n",*p);   //accessing the value
   return 0;
}

%x以十六进制数形式输出整数

直接和间接访问变量内容

在C语言中,有两种等效的方法来访问和操作变量内容

  • 直接访问:我们直接使用变量名
  • 间接访问:我们使用指向变量的指针

例2. 通过指针交换两个变量的值

main ()
{
    int *a,*b,*c; /* Declr ptrs */
    int A,B; /* Declare storage */
    A = 12; /* Initialize storage */
    B = 9;
    a = &A; /* Initialize pointers */
   b = &B;
   printf ("%d %d\n",*a,*b);
   c = a; /* swap pointers */
   a = b;
   b = c;
   printf ("%d %d\n",*a,*b);
}

 

 

例3.指针变量和直接取址运算符和间接取址运算符的应用 (一个程序了解指针和地址)

#include <stdio.h>
int main()
{
   int* pc, c;
   
   c = 22;
   printf("Address of c: %p\n", &c);
   printf("Value of c: %d\n\n", c);  // 22
   
   pc = &c;
   printf("Address of pointer pc: %p\n", pc);
   printf("Content of pointer pc: %d\n\n", *pc); // 22
   
   c = 11;
   printf("Address of pointer pc: %p\n", pc);
   printf("Content of pointer pc: %d\n\n", *pc); // 11
   
   *pc = 2;
   printf("Address of c: %p\n", &c);
   printf("Value of c: %d\n\n", c); // 2
   return 0;
}

结果:

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784
Content of pointer pc: 22

Address of pointer pc: 2686784
Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

程序说明:

int* pc, c;

A pointer variable and a normal variable is created.

在这里,创建了一个指针 pc 和一个普通变量 c,它们都是 int 类型。
由于 pc 和 c 最初未初始化,因此指针 pc 指向无地址或随机地址。而且,变量 c 有一个地址,但包含随机垃圾值。

2.

c = 22;

22 is assigned to variable c.

这将 22 分配给变量 c。也就是说,22存储在变量c的内存位置。

3.

pc = &c;

Address of variable c is assigned to pointer pc.

这将变量 c 的地址分配给指针 pc。

4.

c = 11;

11 is assigned to variable c.

这将 11 分配给变量 c。

5.

*pc = 2;

5 is assigned to pointer variable's address.

将指针 pc 指向的内存位置的值更改为 2

C语言指针应用的常见错误

[content_control]
假设,您希望指针 pc 指向 c 的地址。下列程序是错误的

int c, *pc;

// pc is address but c is not
pc = c; // Error

// &c is address but *pc is not
*pc = &c; // Error

// both &c and pc are addresses
pc = &c; // Not an error

// both c and *pc values
*pc = c; // Not an error

这是初学者经常感到困惑的指针语法示例。

#include <stdio.h>
int main() {
   int c = 5;
   int *p = &c;

   printf("%d", *p);  // 5
   return 0; 
}

为什么我们在使用 int *p = &c; 时没有出错?
这是因为

int *p = &c;

相当于:

int *p:
p = &c;

在这两种情况下,我们都在创建一个指针 p(不是 *p)并将 &c 分配给它。

为了避免这种混淆,我们可以使用如下语句:

int* p = &c;

[/content_control]

 

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C语言教程

Related Posts