Arrays and Pointers in C Examples for GATE Students

Basic Tip :
Between Unary and Binary Operator, the priority of Unary Operator is more than Binary Operator.

How to solve these type of Programs in GATE Exam ?

Program No 1 :

Consider the following Program :

main()
   {
    char *s[] = {"iceman", "green", "cone", "please"};
    char **ptr[] = {s+3, s+2, s+1, s};
    char ***p = ptr;
    printf("%s", **++p);
    printf("\n%s", *--*++p+3);
    printf("\n%c", **--*--p+3);
   }

What will be the output of the above program if assume the program is running on 16 bit processor and the starting addresses of the array is as follows :

base address of s = 5000
base address of "ice" = 1000
base address of "green" = 2000
base address of "come" = 3000
base address of "please" = 4000
base address of ptr = 6000
 Let the address of the variable p = 7000
Output of Program No 1 :
cone
man
j
Explanation of Program No 1 :

Statement 3 :

Arrays and Pointers in C Examples for GATE Students 1

Statement 4 and 5 :

Arrays and Pointers in C Examples for GATE Students 2

Statement 6 :

Arrays and Pointers in C Examples for GATE Students 3

Statement 7 :

Arrays and Pointers in C Examples for GATE Students 4

Statement 8 :

Arrays and Pointers in C Examples for GATE Students 5

Program No 2 :

Consider the following Program :

1. main()
2.    {
3.     char s[] = {'a', 'b', 'c', 'd', 'e', '\0'};
4.     char *p, *str, *str1;
5.     p = &s[3];
6.     str = p;
7.     str1 = s;
8.     printf("%d", ++*p+++*str1-32);
9.    }

What will be the output of the above program if assume the program is running on 16 bit processor and the starting addresses of the array  s is 1000. Let the address of the variable p = 2000, str = 3000, str1 = 4000.

Output of Program No 2 :
167
Explanation of Program No 2 :

Statement 3, 4, 5, 6 and 7 :

Arrays and Pointers in C Examples for GATE Students 6

Statement 8 :

Arrays and Pointers in C Examples for GATE Students 7

Program No 3 :

Consider the following Program :

1. main()
2.    {
3.     char a[] = "GATE2011";
4.     printf("%s", a + a[3] - a[1];
5.    }

What will be the output of the above program?

a) E20011
b) TE2011
c) 2011
d) 011
Answer of Program No 3 :
c) 2011
Explanation of Program No 3 :

Statement 3 and 4 :

Arrays and Pointers in C Examples for GATE Students 8Arrays and Pointers in C Examples for GATE Students 8

     

Incoming search terms:

  • array for gate exam

Leave a Reply