const char*
volatile const char*
char *const
In c under what circumstance is it used? an example will help understand better.
const char*
volatile const char*
char *const
In c under what circumstance is it used? an example will help understand better.
const char *
(and char const *
, both are equivalent) are used to point to characters that are constant, i.e. the string can't be modified.
Example:
const char string1[] = "foo"; // Create a constant, unmodifiable string
const char string2[] = "bar";
const char *s = string1;
// s[0] = 'o'; ERROR: Attempting to modify the constant data
s = string2; // Okay, makes s point to another constant string
char * const
make the pointer variable itself constant, the pointer can't be modified to point somewhere else. The string contents can be modified though.
Example:
char string1[] = "foo";
char string2[] = "bar";
char * const s = string1; // Initialize to point to string in string1
s[0] = 'o'; // Okay, string1 is now "ooo"
// s = string2; ERROR: Attempting to modify a constant variable
These const
qualifiers can be combined:
const char string1[] = "foo";
const char string2[] = "bar";
const char * const s = string1; // Initialize to point to string in string1
// s[0] = 'o'; ERROR: Attempting to modify the constant data
// s = string2; ERROR: Attempting to modify a constant variable
As @Someprogrammerdude has covered the language implications of const
qualifier I will focus on some implementation aspects
Embedded (especially microcontroller) programmers need to control where the data is placed. If you have a lookup table or data which does not change you want to put it into the FLASH memory and not waste the precious RAM.
Most (known to me) embedded implementations will place const
data in the .rodata
segment which usually is physically located in the non-volatile memory (mainly FLASH).
after edit:
const char*
- pointer is not const
, data referenced by the pointer is const
const char const *
- syntax error - means nothingconst char * const
- pointer is const
, data referenced by the pointer is const
volatile const char*
- pointer is not const
, data referenced by the pointer is const
but is side effect prone. It means that you cant change that data, but something can. As an example - the read-only hardware register mapped into the address space.const *char
- it is wrong syntax and it means nothingthere is no constant
in C so I assume you mean const
.
const char const *
is the same as constant char *
so it makes no sense to repeat the const
const *char
is not legal C syntax
Anyway - your main question seeems to be
Why and when should I use
const
in my code
There are several reasons. Using const
gives your compiler some extra information about what you want to do. The compiler can make a number of decisions based on that information like doing some optimization, deciding where in memory to place your object and even catching programming bugs.
Another benefit is that you can provide the same information to users of your code. For instance if you write a function that I'm going to use and do it like:
void foo(char * string);
I can't know whether foo
will change the contents of string
. I'll have to read some additional documentation to find out.
But if you do
void foo(const char * string);
I know that your function foo
will not change the string
that I pass to it. Further, I would know that it's safe to pass a string literal to that function. In other words - by using const
you have given me some extra information in the function proto type and thereby made it easier to use your code.
Another benefit is that it can help you catch mistakes. Let's say you design the function foo
so that it shouldn't modify string
. One year later you (or a co-worker) need to make some updates to the function. Meanwhile you have forgotten (or your co-worker didn't realize) that foo
shouldn't change string
and therefore do stuff like string[8] = 'A'
. If you originally declared foo
using const
, the compiler will issue an error for that code and thereby help you to avoid mistakes.