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

Discussion

What will be the output of the program ?

#include <stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

 

  • A.5
  • B.25
  • C.125
  • D.Garbage value

Answer: B

Correct answer: 25

The expression b = **ptr***ptr; tokenises as **ptr * **ptr - the lexer taking the longest valid token at each point, so the run of asterisks splits into two dereferences, a multiplication, and two more dereferences.

ptr is a pointer to aa, which points to a, so **ptr is 5 and the product is 25. The code relies on the old implicit int return type, which was removed in C99, so it would not compile cleanly under a modern standard.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.