Difference between revisions of "Interview Preparation Pointers"

From Embedded Systems Learning Academy
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
Line 47: Line 47:
 
*p = 5;                        // Finally pointer points to location containing value 5
 
*p = 5;                        // Finally pointer points to location containing value 5
 
</pre>
 
</pre>
 +
 +
'''Pointer to Pointer'''
 +
  We have pointer to int, when we have  a pointer to pointer , our new pointer (n_ptr in this case) contains the address of the pointer (ptr in this case), which in turn points to the int variable.
 +
 
 +
<pre>
 +
Code :
 +
#include <stdio.h>
 +
 +
int main () {
 +
  int  i;
 +
  int  *ptr;
 +
  int  **n_ptr;
 +
 +
  i = 5;
 +
  /* take the address of var */
 +
  ptr = &i;
 +
 +
  /* take the address of ptr using address of operator & */
 +
  n_ptr = &ptr;
 +
 +
  /* take the value using pptr */
 +
  printf("Value of i = %d\n", i );
 +
  printf("Address of i = %d\n", &i );
 +
  printf("Value available at *ptr = %d\n", *ptr );
 +
  printf("Value available at ptr = %d\n", ptr );
 +
  printf("Address available of  ptr = %d\n", &ptr );
 +
  printf("Value available at **n_ptr = %d\n", **n_ptr);
 +
  printf("Value available in n_ptr = %d\n", n_ptr);
 +
  printf("Value available in *n_ptr = %d\n", *n_ptr);
 +
  return 0;
 +
}
 +
 +
Output :
 +
 +
Value of i = 5
 +
Address of i = -1539864
 +
Value available at *ptr = 5
 +
Value available at ptr = -1539864
 +
Address available of  ptr = -1539860
 +
Value available at **n_ptr = 5
 +
Value available in *n_ptr = -1539864
 +
Value available in n_ptr = -1539860
 +
</pre>
 +
  
 
'''Passing Pointers to a Function'''
 
'''Passing Pointers to a Function'''
Line 122: Line 166:
  
 
The Max value is 200
 
The Max value is 200
 +
</pre>
 +
 +
 +
'''Array of Pointers'''
 +
  There can be a situation where we have to maintain array , which stores pointers to int or char or any-other datatype. This can be done as follows.
 +
 +
<pre>
 +
Code :
 +
 +
#include <stdio.h>
 +
 +
int main () {
 +
 +
  int  var[] = {10, 100, 200};
 +
  int i = 0, *ptr = var;
 +
 +
  /* Print the values of array var */
 +
  printf("Value of var[%d] = %d\n", i++, *(ptr++) );
 +
  printf("Value of var[%d] = %d\n", i++, *(ptr++) );
 +
  printf("Value of var[%d] = %d\n", i, *ptr );
 +
  return 0;
 +
}
 +
 +
Output :
 +
Value of var[0] = 10
 +
Value of var[1] = 100
 +
Value of var[2] = 200   
 
</pre>
 
</pre>

Latest revision as of 19:07, 18 December 2016

Pointers : A pointer is a variable who's value is address of some other variable i.e. it can be address of some memory location.

<varaible_type> *<name>
eg : 
     int *pointer_to_integer

The above example , we have declared a pointer to a variable (pointer_to_integer), the variable stores the address of an integer .

Implementation of Pointer :

Code:

#include <stdio.h>
int main(void) {
	int var = 20;
	int *p;
	p = &var;
	
	printf("Pointer %d\n",var);
	printf("Pointer %d\n",&var);     // Prints the address of the varaible (var)
	printf("Pointer %d\n",p);        
	printf("Pointer %d\n",*p);	     // Prints the value that (p) points to   
	return 0;
}

Output :

Pointer 20
Pointer 1809844068
Pointer 1809844068
Pointer 20

Malloc

  The Malloc function dynamically allocates memory when required. The function allocates size of byte of memory and returns a pointer to the first byte of NULL.
Syntax:
pointer = (type)malloc(size in bytes);
 
Code:

int* p;                        //Declare pointer                          
p = (int*)malloc(sizeof(int)); // Pointer equal to pointer type int that contain memory address space of int
*p = 5;                        // Finally pointer points to location containing value 5

Pointer to Pointer

  We have pointer to int, when we have  a pointer to pointer , our new pointer (n_ptr in this case) contains the address of the pointer (ptr in this case), which in turn points to the int variable.
 
Code :
#include <stdio.h>
 
int main () {
   int  i;
   int  *ptr;
   int  **n_ptr;

   i = 5;
   /* take the address of var */
   ptr = &i;

   /* take the address of ptr using address of operator & */
   n_ptr = &ptr;

   /* take the value using pptr */
   printf("Value of i = %d\n", i );
   printf("Address of i = %d\n", &i );
   printf("Value available at *ptr = %d\n", *ptr );
   printf("Value available at ptr = %d\n", ptr );
   printf("Address available of  ptr = %d\n", &ptr );
   printf("Value available at **n_ptr = %d\n", **n_ptr);
   printf("Value available in n_ptr = %d\n", n_ptr);
   printf("Value available in *n_ptr = %d\n", *n_ptr);
   return 0;
}

Output :

Value of i = 5
Address of i = -1539864
Value available at *ptr = 5
Value available at ptr = -1539864
Address available of  ptr = -1539860
Value available at **n_ptr = 5
Value available in *n_ptr = -1539864
Value available in n_ptr = -1539860


Passing Pointers to a Function

  In the code below we have a defines a function values , which return void . It takes two parameters which are pointers to an integer. When the function is called the address of the integer is passed and not the value .    
  
Code:

#include <stdio.h>
 
/* function declaration */
void value(int *num_ptr_1, int *num_ptr_2);
 
int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;
 
   /* calling a function value */
      value(&a, &b);
 
   return 0;
}
 
/* function prints the value of  two numbers */
void value(int *num1, int *num2) {
    
 printf("The value of num1 is %d & num2 is %d", *num1 , *num2 );
    
}

Output :

The value of num1 is 100 & num2 is 200


Passing Pointer from a function

  In the example below returning the address of the local variable outside a function is not a good idea , hence we declare the variable as static . 
Code:
#include <stdio.h>

/* function  return max number. */
int* getMax( ) {

   static int  a = 100;
   static int  b = 200;
 
    if (a > b)
    {
       return &a;        
    }
    else
    {
        return &b;
    }
}
 
/* main function to call above defined function */
int main () {

   /* a pointer to an int */
   int *p;

   p = getMax();
   printf("The Max value is  %d\n", *p);

   return 0;
}

Output :

The Max value is 200


Array of Pointers

  There can be a situation where we have to maintain array , which stores pointers to int or char or any-other datatype. This can be done as follows.
Code :

#include <stdio.h>
 
int main () {

   int  var[] = {10, 100, 200};
   int i = 0, *ptr = var;
 
   /* Print the values of array var */
   printf("Value of var[%d] = %d\n", i++, *(ptr++) );
   printf("Value of var[%d] = %d\n", i++, *(ptr++) );
   printf("Value of var[%d] = %d\n", i, *ptr );
   return 0;
}

Output :
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200