Basic of C Pointers

You can not imagine C language without the pointers. A pointer is the very important concept of C language, so you should have good knowledge of pointer.

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 than 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 name followed by an asterisk (*). Asterisk is a unary operator.

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 result of the operation is value addressed by the operand (pointer).

In another 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”.

 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.

1. Declare a pointer
2. Assigned address to the pointer.
3. Access the pointers.

Let see an example,

In below example, I am creating an integer pointer (iPtr) and using this printer I am accessing an integer variable (data).

 

c pointers

For more info on the pointer, check out: