Why is it possible to store a string in a char pointer?

4

Why does char store a single character and the pointer points to its value in memory, so should it not have only one character? Example:

char *str = "Ola";

Why str does not store just one character?

    
asked by anonymous 17.11.2018 / 17:55

1 answer

4

The question starts from a wrong premise. You are not storing a string in a char pointer.

The string is being stored in a static area of memory, usually the data segment . str is actually storing a pointer to char (4 or 8 bytes according to the architecture), then obviously an address. What is this address? The address where the string begins in the static area of the memory.

Remembering whole string in C has an extra termination character indicating its end . In this example there are 4 bytes in the static area.

Does it make sense now?

    
18.11.2018 / 00:02