C Pointer, Pointer & Array, Pointer & Function

Advertisements

Pointer in C

A normal variable is used to store value.

A pointer variable is used to store address / reference of another variable.

Declaration of Pointer

Syntax for declaring Pointer variable

		Data-type *pointer-name;  

Examples for declaring Pointer variable

		  int A=10;            //    statement 1
		  int *ptr;               //    statement 2
		  ptr = &A;           //    statement 3

In the above example, variable A is value type, it will store and return value. *ptr is reference type variable or we can say *ptr is pointer variable. The astrisk(*) tells compiler that we want a pointer variable that will hold the address of another variable. To retrive the address of A we use "&" (address of ) operator. In statement 3, &A will return the address of A and put it into ptr.

Advantages of Pointer

  • Pointers provide direct access to memory.
  • Pointers allows us to return more than one value from the functions.
  • Reduces the execution time of the program.
  • Pointers provide a way to perform dynamic memory allocation and deallocation.

Disadvantages of Pointer

  • Uninitialized pointers might cause segmentation fault.
  • Dynamically allocated block needs to be freed explicitly.
  • If pointers are updated with incorrect values, it might lead to memory corruption.

Pointer and Array

When we declare an array, compiler allocates continuous blocks of memoy so that all the elements of an array can be stored in that memory. The address of first allocated byte or the address of first element is assigned to an array name. Thus array name workes as pointer variable.

The address of first element is also known as base address.

Example : Displaying elements of an array using pointer.


       #include<stdio.h>

       void main()
       {
              int arr []={78,45,12,89,56,23,79,46,13,82};          // Array of 10 elements
              int *ptr;                                                                 // Pointer variable

              ptr = arr;                                                               // Assigning reference of array in
                                                                                          // pointer variable

              printf("\nValues : ");
              for(int a=1;a<=10;a++)
              {
                   printf("%d, ",*ptr);                                             // Displaying values of array
                                                                                           // using pointer
                   ptr++;                                                               // Incrementing pointer variable
               }
       }

   Output :

              Values : 78, 45, 12, 89, 56, 23, 79, 46, 13, 82,

In the above example statement 1 creates an array of 10 elements. Statement 2 creates a pointer variable ptr. As said above array name workes as pointer variable therefore statement 3 is assigning the address of array in pointer variable ptr. Now ptr have the address of first element in an array. Statement 4 will display the value at address of ptr. After display the first value, statement 5 incrrease the pointer variable ptr to point to next element in an array and statement 4 will display the next value in an array until the loop ends.

Array of Pointers

Array is a collection of values of similar type. It can also be a collection of references of similar type.

Syntax


              Data-type * array [size];
					

Example


       #include<stdio.h>

       void main()
       {
              int x=10,y=20,z=30;
              int *arr[3];                     // Declaring array of three pointer

              arr[0] = &x;                     // Assigning reference of x to array 0th position
              arr[1] = &y;                     // Assigning reference of y to array 1th position
              arr[2] = &z;                     // Assigning reference of z to array 2nd position

              printf("\nValues : ");
              for(int a=0;a<3;a++)
                   printf("%d, ",*arr[a]);

       }

   Output :

              Values : 10, 20, 30,

In the above example, we have declared three variable x, y, z and assigning the addresses of these variables into an array of pointer(*arr[]).

Pointer and Function

Like normal variable, pointer variable can be passed as function argument and function can return pointeras well.

There are two approaches to passing argument to a function:

  • Call by Value
  • Call by Reference / Address

Call by Value

In this approach, the values are passed as function argument to the definition of function.

Example of call by value


       #include<stdio.h>

       void fun(int,int);

       void main()
       {
              int A=10,B=20;

              printf("\nValues before calling %d, %d",A,B);

              fun(A,B);                           //Statement     1

              printf("\nValues after  calling %d, %d",A,B);

       }

       void fun(int X,int Y)                   //Statement     2
       {
              X=11;
              Y=22;
       }

   Output :

              Values before calling 10, 20
              Values after  calling 10, 20

In the above example, statement 1 is passing the values of A and B to the calling function fun(). fun() will recieve the value of A and B and put it into X and Y respectively. X and Y are value type variables and are local to fun(). Any changes made by value type variables X and Y will not effect the values of A and B.



		

Call by Reference

In this approach, the references / addresses are passed as function argument to the definition of function.

Example of call by reference


       #include<stdio.h>

       void fun(int*,int*);

       void main()
       {
              int A=10,B=20;

              printf("\nValues before calling %d, %d",A,B);

              fun(&A,&B);                           //Statement     1

              printf("\nValues after  calling %d, %d",A,B);

       }

       void fun(int *X,int *Y)                   //Statement     2
       {
              *X=11;
              *Y=22;
       }

   Output :

              Values before calling 10, 20
              Values after  calling 11, 22

In the above example, statement 1 is passing the reference of A and B to the calling function fun(). fun() must have pointer formal arguments to recieve the reference of A and B. In statement 2 *X and *Y is recieving the reference A and B. *X and *Y are reference type variables and are local to fun(). Any changes made by reference type variables *X and *Y will change the values of A and B respectively.


		

Difference b/w Call by Value and Call by Reference

Call by ValueCall by Reference
The actual arguments can be variable or constant.The actual arguments can only be variable.
The values of actual argument are sent to formal argument which are normal variables.The reference of actual argument are sent to formal argument which are pointer variables.
Any changes made by formal arguments will not reflect to actual arguments.Any changes made by formal arguments will reflect to actual arguments.

Function Returning a Pointer

Like normal variable, a function can also return reference or address. When function returns reference or address, the return type of function must be pointer type.

Syntax for function returning pointer:


              return-type *function-name(argument list)
              {
                           - - - - - - - - - -
                           body of function
                           - - - - - - - - - -
              }

Example for function returning pointer:


       #include<stdio.h>

       int *reference(int);

       void main()
       {
              int A=10;
              int *ptr;

              printf("\nAddress of %d in main() is %u",A,&A);
              ptr = reference(A);
              printf("\nAddress of %d in reference() was %u",A,ptr);
       }

       int *reference(int n)
       {
              return &n;
       }

   Output :

              Address of 10 in main() is 8736
              Address of 10 in reference() was 8730

Pointer to Function

Like normal variable, Every function has reference or address, and if we know the reference or address of function, we can access the function using its reference or address. This is the another way of accessing function using pointer.

Syntax for declaring a Pointer to function:


              return-type (*ptr-function)(argument list);

return-type : type of value function will return.

argument list : represents the type and number of value function will take, values are sent by the calling statement.

(*ptr-function) : The parentheses around *ptr-function tells the compiler that it is pointer to function.

If we write *ptr-function without parentheses then it tells the compiler that ptr-function is a function that will return a pointer.

The pointer to function will be accessed as:


              (*ptr-function)(val1,val2...n);

Example : Sum of two numbers using pointer to function.


       #include<stdio.h>

       int Sum(int,int);
       int (*ptr)(int,int);

       void main()
       {
              int a,b,rs;

              printf("\nEnter 1st number : ");
              scanf("%d",&a);

              printf("\nEnter 2nd number : ");
              scanf("%d",&b);

              ptr = Sum;                 //Statement   1
              rs = (*ptr)(a,b);          //Statement   2

              printf("\nThe sum is : %d",rs);
       }

       int Sum(int x,int y)
       {
              return x + y;
       }

   Output :

              Enter 1st number : 78
              Enter 2nd number : 45
              The sum is : 123

In the above program, Statement 1 is assigning the address of Sum() to pointer ptr. Statement 2 is calling and passing two values to Sum().

Related topics

Advertisement