Basic Programs on Pointers in C for GATE Students

Before Solving the below Programs, read out the basics of Pointers in the following link :

Link : http://www.edugrabs.com/pointers-in-c-for-gate-students/

Program No 1 :

What will be the output of the following program ? Assume processor is of 16 bit.

1. main()
2.    {
3.     char *p;              
4.     printf("%d, %d", sizeof(*p), sizeof(p));
5.    }
Output of Program No 1 :
1, 2
Explanation of Program No 1 :

Basic Programs on Pointers in C for Gate Students

The compiler dependent problems will never come in GATE exam. The size of the address is always given in the exam before asking the question.

  • If the processor is of 16 bit, then size of address is 2.
  • If the processor is of 32 bit, then size of address is 4.
  • If the processor is of 64 bit, then size of address is 8.

Program No 2 :

What will be the output of the following program ? Assume processor is of 16 bit.

1. main()
2.    {
3.     int *p;              
4.     printf("%d, %d", sizeof(*p), sizeof(p));
5.    }
Output of Program No 2 :
2, 2

Program No 3 :

What will be the output of the following program ? Assume processor is of 16 bit.

1. main()
2.    {
3.     float *p;              
4.     printf("%d, %d", sizeof(*p), sizeof(p));
5.    }
Output of Program No 3 :
4, 2

Program No 4 :

What will be the output of the following program ? Assume processor is of 16 bit and the variable p is stored at the address 2000, the starting address of array a is 3000.

1. main()
2.    {
3.     char *p = "GOOD";
4.     char a[] = "GOOD";              
5.     printf("%d, %d, %d", sizeof(p), sizeof(*p), strlen(p));
6.     printf("\n%d, %d, sizeof(a), strlen(a));
5.    }
Output of Program No 4 :
2, 1, 4
5, 4
Explanation of Program No 4 :

Statement 3 :

Basic Programs on Pointers in C for Gate Students 4.1

Statement 4 :

Basic Programs on Pointers in C for Gate Students 4.1

Statement 5 :

Basic Programs on Pointers in C for Gate Students 4.2

Statement 6 :

Basic Programs on Pointers in C for Gate Students 4.3

Program No 5 :

What will be the output of the following program ? Assume processor is of 16 bit and the starting address of array a and p is 1000 and 2000 respectively. The variable ptr is stored at athe address 3000.

1. main()
2.    {
3.     int a[] = {0,1,2,3,4};
4.     int *p[] = {a,a+1,a+2,a+3,a+4};
5.     int **ptr = p;
6.     printf("%u, %d",a,*a);                
7.     printf("\n%u, %u, %d", p, *p, **p);
8.     printf("\n%u, %u, %d, ptr, *ptr, **ptr);
9.    }
Output of Program No 5 :
1000, 0
2000, 1000, 0
2000, 1000, 0
Explanation of Program No 5 :

Statement 3 and 4 :

Basic Programs on Pointers in C for Gate Students

Statement 5 and 6 :

Basic Programs on Pointers in C for Gate Students

Statement 7 and 8 :

Basic Programs on Pointers in C for Gate Students

     

Leave a Reply