edited by
2,448 views
1 votes
1 votes
Consider the following c-program:

#include<stdio.h>

int main() {

char *arr[ ] ={"GATE", "CAT", "IES", "IAS", "PSU", "IFS" };

call(arr);

return 0;

}

void call (char **ptr) {

char **ptr1;

ptr1=(ptr+=size of (int)) -2;

printf("%s\n",*ptr1);

}

which of the following represent the output of the above program?(Assume size of int,pointer is 4B).

A)IES

B)IAS

C)CAT

D)PSU
edited by

2 Answers

2 votes
2 votes
char *arr[ ] ={"GATE", "CAT", "IES", "IAS", "PSU", "IFS" };

Above is a array of character pointers.

Size of a char* = 8 bytes ( normally in a 64-bit system )

let arr = 1000, then arr +1 = 1008 and arr + 2 = 1016

ptr1=(ptr+=size of (int)) -2;

sizeof( int ) = 4

ptr += 4, ptr = ptr + 4, ptr = 1000 + (4*8); ( 8 is the size of character pointer )

ptr = 1032

ptr1 = 1032 - 2 => ptr = 1032 - (2*8) => ptr = 1016

ptr == (arr + 2)

So, 

printf("%s\n",*ptr1)

prints " IES " ( third element of the array )

0 votes
0 votes

This is my approach to solving it please tell if I am wrong

Related questions

5 votes
5 votes
1 answer
4
Markzuck asked Dec 22, 2018
1,247 views
someone please explain this:how does a+1 differs from &a+1 in above code?detailed explanation would be of great help as they incremented &a by 6 and NOT 1