Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to const string in C

char *p = "string"; //creates pointer to constant string

char p[] = "string"; //just an array with "string"

I'm just a bit confused about why does it in the first example create a pointer to a constant string? Shouldn't it be just a pointer refering to a place in memory with "string"?

like image 969
Mihai Neacsu Avatar asked Jun 29 '11 09:06

Mihai Neacsu


People also ask

How do you declare a pointer to a constant string?

We declare a pointer to constant. First, we assign the address of variable 'a' to the pointer 'ptr'. Then, we assign the address of variable 'b' to the pointer 'ptr'. Lastly, we try to print the value of 'ptr'.

Can a pointer point to a const?

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.

What is constant C string?

String Literals. A String Literal, also known as a string constant or constant string, is a string of characters enclosed in double quotes, such as "To err is human - To really foul things up requires a computer." String literals are stored in C as an array of chars, terminted by a null byte.

What is constant pointer and pointer to constant in C?

A constant pointer is declared as : int *const ptr ( the location of 'const' make the pointer 'ptr' as constant pointer) 2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.


2 Answers

In the first case, "string" may be stored in a read-only area of the process, so attempting to modify the memory pointed to by p would cause undefined behaviour.

In the second case, actual memory is allocated and initialised on the stack at runtime (or in an appropriate section of the process at program startup if it's not a local variable), so modifying the memory is OK.

The first case can be illustrated like this:

+---+           +---+---+---+---+---+---+---+
|   |   ---->   | s | t | r | i | n | g | \0|
+---+           +---+---+---+---+---+---+---+
  p

Whereas the second case is:

+---+---+---+---+---+---+---+
| s | t | r | i | n | g | \0|
+---+---+---+---+---+---+---+
              p

The two declarations have a significant difference, although you may have heard that they are the same from low-quality C programming books.

The first is a pointer of type char *, whereas the second is an array of type char []. Array identifiers decay to a pointer in some contexts, but that doesn't make them pointers.

A pointer is merely an address and an array is "the whole thing" - in the first case sizeof(p) yields the size of a pointer (usually 4 or 8 depending on the target machine), and in the second case it yields 7 * sizeof(char), the length of the actual string.

like image 145
Blagovest Buyukliev Avatar answered Oct 05 '22 16:10

Blagovest Buyukliev


It is unfortunately legal in C (and in C++03, for compatibility). But any attempt to modify the string literal via the pointer will result in Undefined behavior. So, better always assign the string literal to a const char*

const char * cp = "Hello"; //OK
char* p = "Hello"; //OK in C and C++03 (unfortunately), Illegal in C++11
cp[0] = 'Y'; //Compile-time error, good
p[0] = 'Y'; //no compiler error, undefined behavior
like image 44
Armen Tsirunyan Avatar answered Oct 05 '22 14:10

Armen Tsirunyan