Difference between revisions of "Interview Preparation topic : Pointers in C"

From Embedded Systems Learning Academy
Jump to: navigation, search
m (Fix the table format in Q.5)
 
(11 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
Lets Look at the following example:
 
Lets Look at the following example:
  
#include <stdio.h>
+
  #include <stdio.h>
int main ()
+
  int main ()
{
+
  {
 
   int  temp;
 
   int  temp;
 
   int temp1;
 
   int temp1;
Line 13: Line 13:
 
   printf("Address of temp1 variable: %x\n", &temp1 );
 
   printf("Address of temp1 variable: %x\n", &temp1 );
 
   return 0;
 
   return 0;
}
+
  }
  
 
When the following code is compiled and executed:<br>
 
When the following code is compiled and executed:<br>
Line 27: Line 27:
 
Consider the following example:<br>
 
Consider the following example:<br>
  
#include <stdio.h>
+
  #include <stdio.h>
int main () {
+
  int main ()
   int  temp= 100;                                    /* Stores the value 20 in variable temp */
+
  {
 +
   int  temp= 100;                                    /* Stores the value 100 in variable temp */
 
   int  *ptr;                                          /* Declares a pointer variable of type int */
 
   int  *ptr;                                          /* Declares a pointer variable of type int */
   ip = &temp;                                        /*store address of temp in pointer variable, so now the address of temp will be in ptr */
+
   ptr = &temp;                                        /*store address of temp in pointer variable, so now the address of temp will be in ptr */
 
   printf("Address of temp variable: %x\n", &temp);    /* print the address of variable temp */
 
   printf("Address of temp variable: %x\n", &temp);    /* print the address of variable temp */
 
   printf("Address stored in ptr variable: %x\n",ptr ); /* prints the address stored in variable ptr */
 
   printf("Address stored in ptr variable: %x\n",ptr ); /* prints the address stored in variable ptr */
 
   printf("Value of *ptr variable: %d\n", *ptr );      /* prints the value stored at address ptr, this is also known as dereferencing of a pointer */
 
   printf("Value of *ptr variable: %d\n", *ptr );      /* prints the value stored at address ptr, this is also known as dereferencing of a pointer */
 
   return 0;
 
   return 0;
}
+
  }
  
 
When we compile and execute the following code:<br>
 
When we compile and execute the following code:<br>
Line 63: Line 64:
  
 
4) What is the difference between *ptr++ and ++*ptr?<br>
 
4) What is the difference between *ptr++ and ++*ptr?<br>
-> ptr++ means  
+
-> ptr++ means Increments the Pointer not Value Pointed by It, ++*ptr means Increments the Value being Pointed to by ptr
  Increments the Pointer not Value Pointed by It
 
  ++*ptr means  
 
  Increments the Value being Pointed to by ptr
 
  
5) What is the difference between constant to pointer and pointer to constant?<br>
+
5) What is the difference between pointer to constant and constant pointer?<br>
->   No Pointer to Constant                                   Constant Pointers
+
<br>
       1 *ptr = 20 Statement is Invalid in Pointer to              1) *ptr = 20 is Absolutely Valid in Constant Pointers i.e Assigning Value is Perfectly legal
+
->
          Constant i.e Assigning Value is Illegal
+
              Pointer to Constant                                           Constant Pointer
       2   ptr ++  Statement  is Valid in Pointer to Constant       2) ptr ++  Statement  is invalid in Constant Pointers
+
       1) *ptr = 20 Statement is Invalid in Pointer to              1) *ptr = 20 is Absolutely Valid in Constant Pointers i.e Assigning Value is  
       3   Declaration : const int *ptr ;
+
        Constant i.e Assigning Value is Illegal                     Perfectly legal
 +
       2) ptr ++  Statement  is Valid in Pointer to Constant       2) ptr ++  Statement  is invalid in Constant Pointers
 +
       3) Declaration : const int *ptr ;                            3) int *const ptr2;
  
 
6) What will be the size of pointer?<br>
 
6) What will be the size of pointer?<br>
 
-> Size of Pointer Variable does not depends upon data type. Pointer variable only stores the address of the variable and Address of variable is of integer type. So Size of Pointers variables of any type is 2 byte or 4 bytes.
 
-> Size of Pointer Variable does not depends upon data type. Pointer variable only stores the address of the variable and Address of variable is of integer type. So Size of Pointers variables of any type is 2 byte or 4 bytes.
 +
 +
7) What do you mean by function to a pointer?<br>
 +
-> Consider the following code:
 +
 +
    #include <stdio.h>;      //Line 1
 +
    void sayHello();        //Line 2
 +
    void sayHello() {        //Line 3
 +
    printf("hello world");}  //Line 4
 +
    int main() {            //Line 5
 +
    void (*sayHelloPtr)() = sayHello; //Line 6
 +
    (*sayHelloPtr)();      //Line 7
 +
  }
 +
 +
  Let us start with line 6. 1)Keyword void is used to state that the function returns nothing.
 +
                            2)We have a pointer name sayHelloptr. This is similar to creating a pointer name.
 +
                            3) * signifies that it is a function to the pointer.
 +
                            4) Parenthesis () indicates that it is a function pointer.
 +
                            5) Since there are no parameters the parenthesis are empty.
 +
                            6) We now have assigned the sayHello function to the function pointer, this is similar to int* x=&z; where we assign the address of z to an int pointer.<br>
 +
                    line 7. 1) Here we dereference the pointer,by  by using the value-at-address (*) operator.
 +
                            2) Again we use the parenthesis() around the pointer to indicate dereferencing of function pointer

Latest revision as of 01:51, 22 May 2018

What is a Pointer?

A pointer is a variable which contains the address in memory of another variable. Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.

Lets Look at the following example:

  #include <stdio.h>
  int main ()
  {
  int  temp;
  int temp1;
  printf("Address of temp variable: %x\n", &temp );
  printf("Address of temp1 variable: %x\n", &temp1 );
  return 0;
  }

When the following code is compiled and executed:
Address of temp variable: edf53400
Address of temp1 variable: adf9a5f6


To declare a pointer to a variable do:

type *pointer;

Where type can be int, double, float, char and pointer can be any variable.


Consider the following example:

  #include <stdio.h>
  int main ()
  {
  int  temp= 100;                                     /* Stores the value 100 in variable temp */
  int  *ptr;                                          /* Declares a pointer variable of type int */
  ptr = &temp;                                         /*store address of temp in pointer variable, so now the address of temp will be in ptr */
  printf("Address of temp variable: %x\n", &temp);    /* print the address of variable temp */
  printf("Address stored in ptr variable: %x\n",ptr ); /* prints the address stored in variable ptr */
  printf("Value of *ptr variable: %d\n", *ptr );       /* prints the value stored at address ptr, this is also known as dereferencing of a pointer */
  return 0;
  }

When we compile and execute the following code:
Address of temp variable: a54fae34
Address stored in ptr variable: a54fae34
Value of *ptr variable:100


Interview Questions on pointers :

1) What is a NULL pointer?
-> A pointer pointing to nothing is called as a NULL Pointer Eg: int *x=NULL;

2) What is a dangling pointer>
-> Dangling Pointer is a pointer that doesn’t point to a valid memory location. Initially the pointer holds a valid address but later the held address is released or freed.

3) What is a void pointer?
. -> In C General Purpose Pointer is called as void Pointer. It does not have any data type associated with it and can store address of any type of variable

  for eg void *ptr;    // ptr is declared as Void pointer
         char cnum;
         int inum;
         float fnum;
         ptr = &cnum;  // ptr has address of character data
         ptr = &inum;  // ptr has address of integer data
         ptr = &fnum;  // ptr has address of float data

4) What is the difference between *ptr++ and ++*ptr?
-> ptr++ means Increments the Pointer not Value Pointed by It, ++*ptr means Increments the Value being Pointed to by ptr

5) What is the difference between pointer to constant and constant pointer?

->

              Pointer to Constant                                            Constant Pointer
     1) *ptr = 20 Statement is Invalid in Pointer to              1) *ptr = 20 is Absolutely Valid in Constant Pointers i.e Assigning Value is 
        Constant i.e Assigning Value is Illegal                      Perfectly legal
     2) ptr ++  Statement  is Valid in Pointer to Constant        2) ptr ++  Statement  is invalid in Constant Pointers
     3) Declaration : const int *ptr ;                            3) int *const ptr2;

6) What will be the size of pointer?
-> Size of Pointer Variable does not depends upon data type. Pointer variable only stores the address of the variable and Address of variable is of integer type. So Size of Pointers variables of any type is 2 byte or 4 bytes.

7) What do you mean by function to a pointer?
-> Consider the following code:

    #include <stdio.h>;      //Line 1
    void sayHello();         //Line 2
    void sayHello() {        //Line 3
    printf("hello world");}  //Line 4
    int main() {             //Line 5
    void (*sayHelloPtr)() = sayHello; //Line 6 
    (*sayHelloPtr)();       //Line 7
 }

 Let us start with line 6. 1)Keyword void is used to state that the function returns nothing.
                           2)We have a pointer name sayHelloptr. This is similar to creating a pointer name.
                           3) * signifies that it is a function to the pointer.
                           4) Parenthesis () indicates that it is a function pointer. 
                           5) Since there are no parameters the parenthesis are empty.
                           6) We now have assigned the sayHello function to the function pointer, this is similar to int* x=&z; where we assign the address of z to an int pointer.
line 7. 1) Here we dereference the pointer,by by using the value-at-address (*) operator. 2) Again we use the parenthesis() around the pointer to indicate dereferencing of function pointer