Top

Discussion

Comment on the output of this C code?

#include 
    void main()
    {
        int k = 4;
        int *const p = &k;
        int r = 3;
        p = &r;
        printf("%d", p);
    }

 

  • A.Address of k
  • B.Address of r
  • C.Compile time error
  • D.Adress of k + address of r

Answer: C

Since the pointer p is declared to be constant, trying to assign it with a new value results in an error.

Output:
$ cc pgm11.c
pgm11.c: In function ‘main’:
pgm11.c:7: error: assignment of read-only variable ‘p’
pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’

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

Post your comment