Mega Code Archive

 
Categories / C Tutorial / Pointer
 

Attempting to modify a constant pointer to non-constant data

ptr is a constant pointer to an integer that can be modified through ptr. ptr always points to the same memory location. #include <stdio.h> int main() {    int x;    int y;    int * const ptr = &x;     *ptr = 7; /* allowed: *ptr is not const */   // ptr = &y; /* error: ptr is const; cannot assign new address */    return 0; }