Question discussion

What is the result of compiling and running the following C program?#include int main() { char s[] = "abc"; char *const p = s; p++; printf(…

2557Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the result of compiling and running the following C program?

#include <stdio.h>
int main() {
    char s[] = "abc";
    char *const p = s;
    p++;
    printf("%c", *p);
    return 0;
}
  • Ab
  • Ba
  • CRuntime error
  • DCompilation error
Correct answer: D

Explanation

In char *const p the pointer itself is constant, so p++ tries to modify a read-only variable and the compiler rejects it; it is const char *p that makes the pointed-to data read-only.

Discussion 0

Nobody has replied yet. Start the discussion!