c语言指针理解,指针的概念和演示指针的简单操作

参考资料:https://aticleworld.com/pointers-in-c/

阅读原文更容易理解,建议阅读原文。原文很长,初学者可能需要好几个连续的小时才能阅读完毕。比如:8小时。

不要企图,阅读一两篇文章就想理解一个概念,建议多读点不同人的资料。

阅读前提:

 为更好理解本文,建议先阅读点其他指针资料,以其有点一知半解的概念。相互印证才能理解一个东西。

阅读代码知识准备:

C语言的指针是最重要的工具。它在C语言中起着至关重要的作用。没有指针知识,您就不可能成为完美的C程序员。因此,您应该对指针有很好的了解。

C语言中的指针是什么?
指针类似于变量,但区别在于指针将位置的地址存储在内存中,而变量存储值。换句话说,可以说,指针用于引用内存中的位置。

在C中的指针声明:
像C变量一样,您应该首先声明指针。指针的声明很重要,因为在声明时,您定义了指针的功能。每个指针都有数据类型(预定义或用户定义)和名称,后跟一个星号(*)。星号是一元运算符。

Syntax,
Data_Type * Pointer_Name;

让我们看下面提到的例子,以了解指针的声明。

char *cPtr // pointer to a character
int *iPtr; // pointer to an integer
float *fPtr; // pointer to float
double *dPtr; // pointer to a double

如何在编程中使用指针?

我认为在了解如何使用指针之前,我们应该了解两个重要的一元运算符。这些一元运算符是间接运算符(*)和运算符的地址(&)。

1,*符号可称之为:间接运算符或解除引用运算符(*)

  它是一元运算符,用于指针声明中,并通过指针间接访问值。间接运算符的操作数应为指针,运算结果为操作数(指针)寻址的值。

  换句话说,您可以理解,如果间接操作符的操作数的类型为“类型的指针”,则运算结果的类型为“类型”。

int *iPtr; // Use of indirection operator in the declaration of pointer
a = *iPtr; //Use of indirection operator to read the value of the address pointed by the pointer
*iPtr = a; //Use of indirection operator to write the value to the address pointed by pointer

2,Address of operator ( &)    地址运算符

It is also a unary operator and gives the address of the operand. According to C standard “The operand of the unary & operator shall be either a function designator or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier”.

 它也是一元运算符,给出操作数的地址。根据C标准,“一元&运算符的操作数应该是函数指示符或左值,它指定的对象不是位字段,也不是用寄存器存储类说明符声明的”。

引用C语言中文网的介绍,这个网站的教程非常好,站长是个理想主义者,如果可以,建议支持下他的教程:

取址运算符 & 用来取得其操作数的地址。如果操作数 x 的类型为 T,则表达式 &x 的类型是 T 类型指针(指向x的指针)。”

取址运算符的操作数必须是在内存中可寻址到的地址。换句话说,该运算符只能用于函数或对象(例如左值),而不可以用于位字段,以及那些还未被存储类修饰符 register 声明的内容。”

自己为了理解取址运算符,编写的测试代码,理解上面的第一句话:

#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
    int a=0;
    float b=1.2L;
    char c='A';
    char str[] ="hello world";
    
    printf("&a=%p
",&a);
    printf("&b=%p
",&b); 
    printf("&c=%p
",&c);
    printf("&str=%p
",&str);
    
    int *a_ptr=&a;
    float *b_ptr=&b;
    char *c_ptr=&c;
    char *str_ptr=&str;
    
    printf("

");
    printf("a_prt=%p
",a_ptr);
    printf("b_ptr=%p
",b_ptr);
    printf("c_ptr=%p
",c_ptr);
    printf("str_ptr=%p
",str_ptr);
    return 0;
}
//运算结果=================
&a=000000000062FDFC
&b=000000000062FDF8
&c=000000000062FDF7
&str=000000000062FDE0

a_prt=000000000062FDFC
b_ptr=000000000062FDF8
c_ptr=000000000062FDF7
str_ptr=000000000062FDE0

--------------------------------
Process exited after 5.233 seconds with return value 0
请按任意键继续. . .

现在,我认为我们需要讨论“如何使用指针”这一主题。因此,我们主要需要三个步骤才能在程序中使用指针,下面将介绍这些指针。

1.声明一个指针
2.为指针分配地址。
3.访问指针。

代码如下:

演示代码:
#include <stdio.h>
int main (void)
{
    int  data = 20;   // declaration of variable
    int  *iPtr = NULL; // declaration of pointer
    iPtr = &data;  // Assign address of data to the pointer
    
    printf("Address of data: %p

", &data);
    
    //Address stored in pointer
    printf("Address stored in iPtr: %p

", iPtr);
    
    //Read value from the stored address with help of pointer
    printf("value of *iPtr = %d

", *iPtr );
    
    //Assign value to the stored address with help of pointer
    *iPtr = 5;
    
    //Again Read value from the stored address with help of pointer
    printf("New value of *iPtr = %d

", *iPtr);
    
    printf("data = %d

", data);
    
    return 0;
}

运行结果:

Address of data: 000000000062FE14

Address stored in iPtr: 000000000062FE14

value of *iPtr = 20

New value of *iPtr = 5

data = 5


--------------------------------
Process exited after 0.3941 seconds with return value 0
请按任意键继续. . .

编译器根据变量数据类型将存储分配给变量。

声明变量后,编译器为变量分配一个唯一的地址

google翻译:声明变量后,编译器为变量分配一个唯一的地址,并将该唯一的地址附加到变量名称中。在使用变量名的程序中,我们可以从分配给变量的地址中存储和检索值。

 百度翻译:在声明变量编译器之后,为变量分配一个唯一的地址,并将这个唯一地址附加到变量名中。在使用变量名的程序中,我们可以从分配给变量的地址中存储和检索值。

我们声明一个变量,其数据类型为int,名称为iPtr。

When the compiler sees “int” then it sets the memory (as per the size of integer) to hold the integer value. Now we are able to store the value into the iPtr. Below I am storing 10 into the iPtr. See the image

当编译器看到“ int”时,它将设置内存(根据整数的大小)以保存整数值。现在我们可以将值存储到iPtr中。在下面,我将10存储到iPtr中。看图片

注意:声明指针时,它不会指向任何地方。使用前,必须将其设置为指向某处。因此,通常在声明指针时,我们使用NULL指针初始化该指针。

 

*解运算符

When we use dereference (*) operator 

当我们对lvalue使用解引用(*)运算符时,它将转到地址并从内存访问数据以进行操作。简而言之,解引用(*)运算符将查找地址中存在的值。

 注意:如果是void指针,我们将无法直接使用间接运算符。
 
 
 
 
原文地址:https://www.cnblogs.com/Tpf386/p/13831048.html