Designation C: pointer to an array of characters (string)

Why are pointers to char arrays (i.e. string s) written as follows:

 char *path 

Instead:

 char *path[] 

or something like that?

How can I create a pointer to char , not string ?

+4
source share
3 answers

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

It is possible that char *path semantically points to a "string", but this is only in the interpretation of the data.

In c, strings are often used with char * , but only on the assumption that the pointer refers to the first character of the string, and the rest of the characters follow in memory until you reach the null terminator. This does not change the fact that it is just a pointer to a character.

+8
source

char *path is a pointer to char. It can be used to indicate a single char, as well as to point to a char in an array with zero completion (string)

char *path[] - an array of pointers to char.

A pointer to an array from char will be char (*path)[N] , where N (the size of the array) is part of the type of the pointer. Such pointers are not widely used, since the size of the array must be known at compile time.

+3
source

char *path[] - an array of pointers. char *path is the only pointer.

How can I create a pointer to a char, not a string?

A 'string' in C is just a pointer to a char that has an agreement to start a sequence of characters that ends with '\ 0'. You can declare a pointer to one char in the same way, you just need to take care to use it according to the data it actually points to.

This is similar to the concept that a char in C is an integer with a fairly limited range. You can use this data type as a number, as a true / false value, or as a character that should appear as a character at some point. Interpretation of what the char variable corresponds to the programmer.

Without first class, a full-fledged string data type is what sets C apart from most other high-level languages. I will let you decide for yourself whether it is distinguished by a good or bad way.

+3
source

All Articles