+ 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?

14th Apr 2019, 2:35 AM
Điệp Dương Đức
Điệp Dương Đức - avatar
3 Réponses
+ 3
The ptr pointer can definitely be incremented to point to the second element in the char array and incrementing 10 gives 11.
14th Apr 2019, 4:09 AM
Sonic
Sonic - avatar
0
It is not a pointer that can't be edited but a static array's pointer (ex: char a[25])
14th Apr 2019, 5:48 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar