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

Pointers Questions and Answers

71.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int i=3, *j, k;
    j = &i;
    printf("%d\n", i**j*i+*j);
    return 0;
}

 

Answer: A

Correct answer: 30

The expression i**j*i+*j looks alarming but tokenises unambiguously as i * (*j) * i + (*j): the lexer takes the longest valid token at each point, so ** becomes * followed by unary *.

With i = 3 and *j = 3:

\[ 3 \times 3 \times 3 + 3 = 27 + 3 = 30 \]

Multiplication binds tighter than addition, so no parentheses are needed to get this grouping.

Please log in or register to report this question.

72.

What is the output of this C code?
 

int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

 

Answer: A

Correct answer: Same address

Incrementing x changes the value stored at the address, not the address itself, and ptr was never modified. So both %p conversions print the same thing. The question is testing that a pointer and the object it points to are separate: assigning through the pointer alters one, assigning to the pointer alters the other.

Please log in or register to report this question.

73.

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.

74.

Will the following program give any warning on compilation in TurboC (under DOS)?

#include <stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}

 

Answer: B

Correct answer: No

All four assignments are legal without a cast. C converts implicitly in both directions between void * and any object pointer type, so assigning an int * to a void * and back again raises no diagnostic. It is C++ that requires an explicit cast when converting away from void *, and mixing up the two languages is what makes this question look harder than it is.

Please log in or register to report this question.

75.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}

 

Answer: D

Correct answer: x=31, y=504, z=504

*y++ = *z++ assigns *z to *y - writing 30 back over itself - and then increments both pointers, since ++ in the postfix position binds to the pointer rather than to the dereferenced value.

Pointer arithmetic is scaled by the pointed-to type, so each advances by sizeof(int), that is 4 bytes: from 500 to 504. x++ then makes x 31. Both pointers now point one past x and must not be dereferenced.

Please log in or register to report this question.

76.

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.

77.

Point out the compile time error in the program given below.

#include <stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}

 

Answer: C

Correct answer: No error

The compiler has nothing to object to: x is a pointer to int and 100 is an int, so *x = 100 is well typed. The fault is at run time, not compile time - x was never given a value, so the assignment writes through an indeterminate address. This is the distinction the question is testing: a program can be perfectly legal and still crash.

Please log in or register to report this question.

78.

What is the output of this C code?

void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int **r = &p;
printf("%p %p", *r, a);
}

 

Answer: C

Correct answer: Same address is printed.

r holds the address of p, so *r is p itself, and p was set from a, which decays to the address of its first element. So *r and a print the same address. Note that the levels are now consistent, unlike the previous question, which is why this one compiles.

Please log in or register to report this question.

79.

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.

80.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}

 

Answer: D

Correct answer: ce

The expression s++ +3 uses the original value of s, since ++ here is postfix, and adds 3 to it. s points at the start of "peace", so the argument to printf addresses position 3.

Counting from zero - p, e, a, c - position 3 is the c, so the output is ce. The increment of s itself happens too, but s is never used again. The spacing in s++ +3 is necessary: s+++3 would tokenise differently.

Please log in or register to report this question.

Loading…

Other Subjects

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