How to create a pointer that cannot change the value in C

1 Answer

0 votes
#include <stdio.h> 
  
int main() 
{ 
    int n = 849;
    
    const int* p = &n;
    
    printf("%d\n", *p);
    
    // *p = 12564; // error: assignment of read-only location ā€˜*p’
    
    p++; // Not the memory of the program
    
    printf("%d\n", *p); // The value of the - Not the memory of the program
      
    return 0;
}
  
  
  
/*
  
run:
  
849
-788534288
  
*/

 



answered Apr 3, 2024 by avibootz
edited Apr 3, 2024 by avibootz
...