Pointers

Pointer in C

Pointer is a variable which holds/store the address of another variable of same data type. Pointer is used to allocate memory dynamically i.e. at run time. Pointer provides power and flexibility to the C language.

Syntax : data_type *variable_name;
Example : int *xchar *x;

Here, asterisk ( * ) is denote “x” is pointer variable and not a normal variable.

Pointer operators (&,*) :-

& is reference operator returns the address of the variable.

* is Indirection operator returns the value from the address.

Pointer Arithmetic

As we know, a pointer is a variable that stores a memory address that is a numerical value. We can perform arithmetic operations on a pointer just as we can a numeric value.

There are four arithmetic operators that can be used on pointers :

  1. Increment (++)
  2. Decrement (--)
  3. Addition (-)
  4. subtraction (+)

Parameter Passing 

  1. Call by value
  2. Call by reference

   1. Call by value :

  • In call by value method, the value of the variable is passed to the function as parameter.
  • The value of the actual parameter cannot be modified by formal parameter.
  • Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.

   2. Call by reference :

  • In call by reference method, the address of the variable is passed to the function as parameter.
  • The value of the actual parameter can be modified by formal parameter.
  • Same memory is used for both actual and formal parameters since only address is used by both parameters.

NOTE :
 Actual parameter – This is the argument which is used in function call.
Formal parameter – This is the argument which is used in function definition.

Pointer to Pointer

Pointers are used to store the address of other variables of similar data type. But if you want to store the address of a pointer variable, then you again need a pointer to store it. Thus, “when one pointer variable stores the address of another pointer variable, it is known as Pointer to Pointer variable or Double Pointer.”

 Syntax :-   int **p; // pointer to a pointer which is pointing to an integer.      

Dynamic memory allocation

It enables the C programmer to allocate memory at runtime.

C language offers 4 dynamic memory allocation functions.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

Here we will discuss only two types of dynamic memory allocation function,

1. malloc()

The malloc() function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
The syntax of malloc() function is :
     ptr=(cast-type*)malloc(byte-size). 

2. calloc()

The calloc() function allocates multiple block of requested memory.
It initially initialize all bytes to zero.
It returns NULL if memory is not sufficient.
The syntax of calloc() function is :
     ptr=(cast-type*)calloc(number, byte-size).

 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free