本文已参与「新人创作礼」活动,一起开启掘金创作之路。
General
You can not imagine C language without the C pointers. A pointer is a very important concept of C language, so you should have good knowledge of pointer.
没有指针,您将无法想象C语言。 指针是C语言非常重要的概念,因此您应该对指针有充分的了解。
What is a pointer in c?
A pointer is similar to a variable but the difference is that pointers are store the address of a location in memory and variable stored the value. In other words, we can say, a pointer is used to reference a location in the memory.
When we have used a pointer to store the address in the memory then using the dereferencing techniques we can also get the value from the address which is stored by the pointer.
指针与变量类似,但区别在于指针是将位置的地址存储在内存中,而变量则存储值。 换句话说,可以说,指针用于引用内存中的位置。
当我们使用指针将地址存储在内存中时,然后使用解引用技术,我们还可以从指针存储的地址中获取值。
Syntax:
Declaration of a pointer is very important because at the time of declaration you define the capability of the pointer. Every pointer has the data types (pre-defined or user-defined) and names followed by an asterisk (*). Asterisk is a unary operator.
指针的声明非常重要,因为在声明时,您定义了指针的功能。 每个指针都有数据类型(预定义或用户定义)和名称,后跟一个星号(*)。 星号是一元运算符。
| 1 | Data_Type * Pointer_Name; |
|---|
Let’s see the below-mentioned example to understand the declaration of a pointer.
让我们看下面提到的例子,以了解指针的声明。
char *cPtr // pointer to a character
int *iPtr; // pointer to an integer
float *fPtr; // pointer to a float
double *dPtr; // pointer to a double
How to Use Pointers?
I think before understanding how to use the pointers, we should know the two important unary operators. These unary operators are indirection operator ( * ) and address of operator ( &).
我认为在了解如何使用指针之前,我们应该了解两个重要的一元运算符。 这些一元运算符是间接运算符(*)和运算符的地址(&)。
Indirection operator or Dereference Operator ( * )
It is a unary operator that is used in the declaration of the pointer and accesses a value indirectly, through a pointer. The operand of the indirection operator should be a pointer and the result of the operation is value addressed by the operand (pointer).
In other word you can understand that if the operand of indirection operator has type ‘‘pointer to type’’, the result of the operation has type ‘‘type’’.
它是一元运算符,用于指针的声明中,并通过指针间接访问值。 间接运算符的操作数应为指针,并且运算结果为操作数(指针)寻址的值。
换句话说,您可以理解,如果间接操作符的操作数的类型为“类型的指针”,则运算结果的类型为“类型”。
Let see an example,
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
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标准,“一元&运算符的操作数应为函数标识符或左值,该左值指定不是位字段且未用寄存器存储类说明符声明的对象”。
Let see an example,
int data = 0; // declaration of integer variable
&data => Give the address of the data
int *pData ; // declaration of pointer
&pData => Give the address of the pointer
Now I think we need to come on the topic “how to use pointers”. So we require mainly three steps to use the pointers in the programs these are mention below.
- Declare a pointer
- Assigned address to the pointer.
- Access the pointers.
现在,我认为我们需要讨论“如何使用指针”这一主题。 因此,我们主要需要三个步骤来在程序中使用指针,下面将介绍这些指针。
1. 声明一个指针
2.为指针分配地址。
3.访问指针。
Let see an example,
In the below example, I am creating an integer pointer (iPtr) and using this pointer I am accessing an integer variable (data).
在下面的示例中,我正在创建一个整数指针(iPtr),并使用此指针访问一个整数变量(data)。
#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\n\n", &data);
//Address stored in pointer
printf("Address stored in iPtr: %p\n\n", iPtr);
//Read value from the stored address with help of pointer
printf("value of *iPtr = %d\n\n", *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\n\n", *iPtr);
printf("data = %d\n\n", data);
return 0;
}
Output:
Advantages of C Pointers
-
We can access the memory location with the help of C Pointers.
-
With the help of pointers, we can pass the structure in an efficient way. It helps to reduce stack usage.
-
We can access the array elements with the help of C Pointers.
-
Pointers are used for dynamic memory allocation using the memory management function.
-
It used in complex data structures like linked lists, trees, etc.
-
Using the pointer we can jump from one application to another application.
-
我们可以在C指针的帮助下访问内存位置。
-
借助指针,我们可以高效地传递结构。 它有助于减少堆栈使用量。
-
我们可以在C指针的帮助下访问数组元素。
-
指针用于使用内存管理功能进行动态内存分配。
-
它用于复杂的数据结构,例如链表,树等。
-
使用指针,我们可以从一个应用程序跳转到另一个应用程序。