in Programming in C
1,358 views
7 votes
7 votes
#include <stdio.h>
void f(char**);
int main()
{
    char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
    f(argv);
    return 0;
}
void f(char **p)
{
    char *t;
    t = (p += sizeof(int))[-1];
    printf("%s\n", t);
}

(a) ab (b) cd (c) ef (d) gh

In GATE Exam, if not specified What should be size of integer?

in Programming in C
1.4k views

1 comment

Size of int depends on the machine, and hence the answer can vary on different machines.
0
0

1 Answer

6 votes
6 votes
Best answer

Answer will be D) gh

Here p will initially point to the "ab"

p += sizeof(int)

This will translate into p = p + 4; // Consider size of int is 4 

Now p will point "ij".

(p)[-1];

will translate into *(p - 1), then it will return the address of "gh"

//Why address of "gh", because p is double pointer, hence single pointer will return the address its element.

Then t will have the address of "gh". Hence "gh" will be printed. 

selected by
by

3 Comments

So size of integer = 4 byte   // if not specified.
0
0
If not specified then we will take 4 only. But if specifically say that its 2 byte then only consider that.
1
1
For the practical purpose, we should take sizeof int as 4. But in GATE it will always be specified or else they will give marks for both 2 and 4.
0
0

Related questions