Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use `const` for strings when defining with pointers in C?

For defining arrays with pointers, we write
int *const a;
because arrays have variable values but constant pointers. But for strings, we write
const char *s;.

I don't understand why. Is this correct? Do strings really have constant values and variable pointers in C?

like image 336
amirali Avatar asked Aug 24 '18 11:08

amirali


People also ask

What is the use of const pointer in C?

We can create a pointer to a constant in C, which means that the pointer would point to a constant variable (created using const). We can also create a constant pointer to a constant in C, which means that neither the value of the pointer nor the value of the variable pointed to by the pointer would change.

What does const do to a pointer?

Constant Pointers A constant pointer in C cannot change the address of the variable to which it is pointing, i.e., the address will remain constant. Therefore, we can say that if a constant pointer is pointing to some variable, then it cannot point to any other variable.

What is the use of string constant?

String constant. A string constant provides character information. It is usually enclosed within single quotes as in the following example. In some situations, such as when a string constant is used to provide the name of a fluid to a thermophysical property function, the single quotes are optional.

Can we use const for string?

Note also that you cannot use the const keyword to create a constant object. The const keyword can only be applied to the primitive data types (such as ints, floats, chars, and booleans) and strings.


1 Answers

Consider the following:

char* a;
char* const b = "Hello"
const char* c;
const char* const d = "world";

a just points to a character. That might be the first character of a string, or a single byte. That character can be modified by writing *a='Z' or a[0]='q' or by passing a to a function taking a char* parameter. The pointer itself can also be modified to point at some other characters, e.g. a=b; or a="foo";. This is the most flexible form, and the least safe, because you can do anything with it.

b is a constant pointer to a character. In this case, it's pointing at an array of characters. Those characters can be modified, e.g. b[1]='a'; or *b=0; but b itself is constant, meaning that it cannot ever point at a different part of memory. This form may be used when you have allocated a buffer or array whose contents you want to modify, but which cannot move, since it was allocated by the operating system.

c points to a constant character. You can change what it points at (c="foo"; or c=b;) but you cannot use this pointer to change the characters that it points at -- even if it's pointing at the non-constant buffer b. This form is commonly used when handling pre-generated strings. You can use this variable to store strings you've found, and pass their address around, as long as you don't change the strings themselves.

d is a constant pointer to a constant character. You can't change the characters it's pointing at, and you can't point the pointer itself at a different memory location. All you can do is read the string or individual characters (char e=d[4]; or puts(d);). This form is mainly used when passing around reference strings, for example when naming real objects.

like image 151
Tim Randall Avatar answered Oct 13 '22 14:10

Tim Randall