Question: What is a Pointer? What is its limitations? What is its benefits? How does we use it? What is all operation we can perform using it?
In this article we is going to discover answers to all these questions.
A pointer is a variable that contains the address of another variable, where other variables can be structures, arrays, or other basic types of data. The basic syntax for defining pointers is as follows:
Datatype * identifier;
Datatype represents the address of the type of data stored in the defined pointer variable.
* is an indirect value/dereference symbol.
identifier is a pointer variable name that is defined.
Pointers allow us to change the data stored in memory directly on memory operations, which is a feature of the C language. Whenever we define a pointer to a type of data, we can call this pointer--for example, integer data--a pointer to an integral type of data.
Pointers and arrays are closely related in the C language. The subscript form of an array can be substituted by a pointer form, and the general pointer form is relatively fast. Therefore, the elements of arr[n] I in the array can be represented by * (arr + i). This can be expressed because the array name itself is the address of the first element of the array. But the array name and pointer still have a very important difference. The pointer is a variable,
Ptr=arr;
And
ptr++;
is legal, but the array name is not a variable, such as a statement:
Arr=ptr;
And
arr++;
are illegal, the definitions for arr and PTR are also different, namely int arr[n] and int *ptr.
Pointer manipulation
Now let's look at pointers-related operations. The possible operations are: assigning values to variables pointed to by pointers, adding and subtraction of pointer variables and pointer variables, adding and subtraction of pointers and integers, pointers minus pointers, and using pointers to add and subtract and compare pairs of array elements. The arithmetic of all other pointers itself is illegal, including but not limited to the addition of pointers themselves, multiplication, panning or masking operations, addition and subtraction of floating-point numbers, non-conversion assignments, etc. However, if the two pointers point to the elements of the same array, you can use the binary operation = =,! =, <, >=.
The C language also defines a generalized pointer--void *, which is a null pointer. It can be projected into any type of pointer, by default the return type is the library function malloc (), the void * Type pointer cannot be de-reference (the value pointed to by the pointer) itself, so it needs to be converted to another type of pointer. The following code contains all the aspects that we have discussed.
#include <stdio.h>intMainvoid){ //declaring a void pointer void*vptr; //Creating some pointer variables intarr[5]={ the,5, -, the,1}; int*ptr1,*Ptr,num,i; Vptr=arr; PTR=(int*) vptr;//Casting void *PTR1=arr+3;//pointer and integer addition if(Ptr1 > PTR)//Comparison of PointersNum=ptr1-ptr;//Subtraction of pointersprintf ("% d,% d,% d\n", *ptr,*ptr1,num); PTR= ptr1-2;//pointer and integer subtractionptr1++;//pointer Incrementprintf ("% d,% d\n", *ptr,*ptr1); return 0;}
Output:
34 39 3
5 1
Here are some illegal operations:
// Invalid operationsptr1 = arr ++ =ptr;ptr *=3/=ptr; char *cptr ==* (arr +4.2<<2;
This article gives beginners an introduction to the basic operation of pointers, multidimensional pointers and pointers to functions we will discuss later.
Play-round pointer (Playing with pointers)