+ 1
Problem with pointer to string in C
#include <stdio.h> #define INCREMENT(x) ++x int main() { char *ptr = "GeeksQuiz"; int x = 10; printf("%s ", INCREMENT(ptr)); printf("%d", INCREMENT(x)); return 0; } // Output: eeksQuiz 11 My question: in that program, the pointer to "GeeksQuiz" is increased by 1, but i was taught that the pointer to an array (more specifically the pointer to the first element of an array) should be treated as a constant and such operation as ptr=(some value) or ptr++ is nonsense. Despite that, the program still run fine and output true results, why?
2 Respuestas
+ 3
The ptr pointer can definitely be incremented to point to the second element in the char array and incrementing 10 gives 11.
0
It is not a pointer that can't be edited but a static array's pointer (ex: char a[25])