const int* ptr;
declares ptr
a pointer to const int
type. You can modify ptr
itself but the object pointed to by ptr
shall not be modified.
const int a = 10; const int* ptr = &a; *ptr = 5; // wrong ptr++; // right
While
int * const ptr;
declares ptr
a const
pointer to int
type. You are not allowed to modify ptr
but the object pointed to by ptr
can be modified.
int a = 10; int *const ptr = &a; *ptr = 5; // right ptr++; // wrong
Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):
int const *ptr; // ptr is a pointer to constant int int *const ptr; // ptr is a constant pointer to int
Related Posts:
- Difference between char* and const char*?
- Pointer Arithmetic
- char *array and char array[]
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- What is the difference between char s[] and char *s?
- What is the difference between const int*, const int * const, and int const *?
- What is the difference between const int*, const int * const, and int const *?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Invalid pointer error on invoking free() after malloc in C
- expression must have integral type
- What’s wrong with my code? What is argv[1]?
- Valgrind: Invalid read of size 1
- Reversing a string in C
- Warning: return from incompatible pointer type in C
- strcmp giving segmentation fault
- Why am I getting this error: “data definition has no type or storage class”?
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- What is the difference between char array and char pointer in C?
- typedef struct pointer definition
- Dereference void pointer
- The difference between char * and char[] [duplicate]
- lvalue required as increment operand
- How big can a 64 bit unsigned integer be?
- How to use execvp()
- How to use execvp() to execute a command
- How does strtok() split the string into tokens in C?
- What is *(uint32_t*)?
- what is Segmentation fault (core dumped)? [duplicate]
- How to convert an int to string in C?
- How to print in C
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- makefile:4: *** missing separator. Stop
- Stack smashing detected
- How do I determine the size of my array in C?
- What is the difference between C and embedded C?
- Undefined reference to pthread_create
- Difference between malloc and calloc?
- What is Bit Masking?
- segmentation fault : 11
- What are .a and .so files?
- So what does “return 0” actually mean?
- what is the meaning of == sign?
- How do I use extern to share variables between source files?
- Cannot assign requested address – possible causes?
- ARM Assembler – How do I use CMP, BLT and BGT?
- Invalid read of size 8 – Valgrind + C
- error: called object is not a function or function pointer
- munmap_chunk(): invalid pointer
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- What is the difference between a static and const variable?
- Return char[]/string from a function [duplicate]
- Is there a printf converter to print in binary format?
- What is a bus error? Is it different from a segmentation fault?
- Output single character in C
- Get the current time in C
- Does connect() block for TCP socket?
- Converting a C program to MIPS
- C: linker command failed with exit code 1
- What’s the equivalent of new/delete of C++ in C?
- sizeof float (3.0) vs (3.0f)
- Get text from user input using C
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to trigger SIGUSR1 and SIGUSR2?
- How do I lowercase a string in C?
- warning: expression result unused
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- Display value found at given address gdb
- Array definition – Expression must have a constant value
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- What is the cause of flexible array member not at end of struct error?
- Variable warning set but not used
- Expected declaration specifier error in function
- How can I get argv[] as int?
- Is the sizeof(some pointer) always equal to four?
- Why I do get “Cannot find bound of current function” when I overwrite the ret address of a vulnerable program?
- Warning: assignment from incompatible pointer type
- strcmp not working
- error: aggregate value used where an integer was expected
- Invalid type argument of -> C structs
- Does C have a string type?
- How does the strtok function in C work? [duplicate]
- error: function returns address of local variable
- Return a `struct` from a function in C
- C: scanf to array
- What are “prototypes” in a C program?
- Realloc Invalid Pointer in C
- What’s the difference between “mod” and “remainder”?
- undefined reference to `std::ios_base::Init::Init()’
- previous declaration of ‘function’ was here in C [duplicate]
- Difference between the int * i and int** i
- Pointer to 2D arrays in C
- Pointer to a string in C?
- How to Compare 2 Character Arrays [duplicate]