Machine-translated by Google. Questions are authored in English.
Top

Pointers Questions and Answers

41.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%s\n", str);
    return 0;
}

 

Answer: A

Correct answer: Mello

char *const p makes the pointer const, not what it points to. So p cannot be made to point elsewhere, but *p = 'M' is perfectly legal and writes through it.

str is a modifiable array, so the write succeeds and the string becomes Mello. Compare const char *p, where the qualifier applies to the pointed-to characters and the assignment would be rejected - the position of const relative to the asterisk decides which is protected.

Please log in or register to report this question.

42.

What will be the output of the program If the integer is 4bytes long?

#include <stdio.h>

int main()
{
    int ***r, **q, *p, i=8;
    p = &i;
    q = &p;
    r = &q;
    printf("%d, %d, %d\n", *p, **q, ***r);
    return 0;
}

 

Answer: A

Correct answer: 8, 8, 8

Each level of indirection is undone by one dereference. p points to i, q points to p, and r points to q.

So *p reaches i directly; **q goes through p to i; and ***r goes through q and p to i. All three arrive at the same object and print 8. The distractors offer addresses, which would appear only if fewer dereferences were applied than there are levels of indirection.

Please log in or register to report this question.

43.

What will be the output of the program ?

#include <stdio.h>

void fun(void *p);
int i;

int main()
{
    void *vptr;
    vptr = &i;
    fun(vptr);
    return 0;
}
void fun(void *p)
{
    int **q;
    q = (int**)&p;
    printf("%d\n", **q);
}

 

Answer: D

Correct answer: 0

i is a global, so it has static storage duration and is zero-initialised before the program starts.

Inside fun, &p is the address of the parameter, which itself holds &i. Casting that to int ** and applying ** therefore reads first the parameter, obtaining &i, and then i itself - giving 0. The double cast is a roundabout way of reaching a value that *(int*)p would have given directly.

Please log in or register to report this question.

44.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *str;
    str = "%s";
    printf(str, "K\n");
    return 0;
}

 

Answer: C

Correct answer: K

printf takes its format as an ordinary const char * argument, so nothing requires it to be a literal written at the call site. Here str holds "%s", and printf(str, "K\n") uses it exactly as if the literal had been written there.

The output is K followed by a newline. The technique is legal but dangerous in practice: a format string taken from input rather than fixed in the source is the classic format string vulnerability, which is why compilers warn about non-literal formats.

Please log in or register to report this question.

45.

What will be the output of the program ?

#include <stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d\n", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}

 

Answer: D

Correct answer: Error: cannot use static for function parameters

The only storage class specifier permitted on a function parameter is register. Declaring a parameter static is a constraint violation, and the compiler rejects both the prototype and the definition.

The reason is that a parameter's storage duration is fixed by the call: it comes into existence when the function is entered and vanishes when it returns. static would demand storage lasting the whole program, which is incompatible with being a parameter at all.

Please log in or register to report this question.

46.

What will be the output of the program if the size of pointer is 4-bytes?

#include <stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}

 

Answer: C

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Please log in or register to report this question.

47.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

 

Answer: D

Correct answer: JACK

Take the three prints in turn. ch is 74, the code for J, so the first prints J. Then j is 65 and *(int*)vp yields 65, which %c prints as A.

Finally vp is set to cp, and (char*)vp+2 advances two characters into "JACK", so %s prints CK. The three outputs concatenate to JACK - a coincidence engineered by the question rather than anything the code copies.

Please log in or register to report this question.

48.

What will be the output of the program?

#include <stdio.h>

int main()
{
    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
    int *p, *q;
    p = &arr[1][1][1];
    q = (int*) arr;
    printf("%d, %d\n", *p, *q);
    return 0;
}

 

Answer: A

Correct answer: 8, 10

The initialiser fills the three-dimensional array in row-major order, so the eight values map to arr[0][0][0] through arr[1][1][1] in sequence: 10, 2, 3, 4, 5, 6, 7, 8.

The last of them, 8, is at arr[1][1][1], which is where p points. And q, cast from the array name, points to the very first element, which holds 10. So the output is 8, 10.

Please log in or register to report this question.

49.

What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?

#include <stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}

 

Answer: C

Correct answer: 1006, 2, 2

Take the three arguments in turn. a[0] is a pointer to the first row's first element, at 1002, and adding 1 advances it by sizeof(int), giving 1006.

*(a[0]+1) dereferences that, yielding a[0][1], which is 2. And *(*(a+0)+1) is the same expression written with explicit pointer arithmetic - a+0 is a, *(a+0) is a[0] - so it too gives 2.

Please log in or register to report this question.

50.

What will be the output of the program?

#include <stdio.h>

int main()
{
    int arr[3] = {2, 3, 4};
    char *p;
    p = arr;
    p = (char*)((int*)(p));
    printf("%d, ", *p);
    p = (int*)(p+1);
    printf("%d", *p);
    return 0;
}

 

Answer: B

Correct answer: 2, 0

p is a char *, so dereferencing it reads a single byte. The first element of the array holds 2, and on a little-endian machine the low-order byte comes first, so *p is 2.

After p+1, the pointer addresses the second byte of that same integer - not the next element, since char arithmetic advances by one byte. For the value 2 in a four-byte int, that byte is zero. The casts are decorative; what matters is that arithmetic is scaled by the pointer's type.

Please log in or register to report this question.

Loading…
Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming