Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C notation: pointer to array of chars (string)

Why are pointers to arrays of chars (ie. strings) written as below:

char *path

Instead of:

char *path[]

or something like that?

How could I create a pointer to a char and not a string?

like image 612
thom Avatar asked Dec 06 '22 21:12

thom


1 Answers

char *path is not a pointer to a string, it is a pointer to a char.

It may be the case that char *path semantically points to a "string", but that is just in the interpretation of the data.

In c, strings often use char *, but only under the assumption that the pointer is to the first character of the string, and the other characters are subsequent in memory until you reach a null terminator. That does not change the fact that it is just a pointer to a character.

like image 89
Gavin H Avatar answered Dec 25 '22 07:12

Gavin H