What is char *?

What is char* , exactly? Is this a pointer? I thought that pointers have an asterisk in front of the identifier, and not a type (which is not necessarily the same thing) ...?

+6
c ++ pointers types
source share
4 answers

This is a pointer to char .

When declaring a pointer, an asterisk follows the type and before the identifier, and spaces are not significant. All declare char pointers:

 char *pointer1; char* pointer2; char * pointer3; char*pointer4; // This is illegible, but legal! 

To make things even more confusing, when declaring several variables at once, the asterisk is applied to only one identifier (on the right). For example:.

 char* foo, bar; // foo is a pointer to a char, but bar is just a char 

First of all, for this reason, an asterisk is conditionally placed immediately next to the identifier, and not with the type, since it avoids this confusing declaration.

+19
source share

This is a pointer to a character. You can write either

 char* bla; 

or

 char *bla; 

Same.

Now, in C, a char pointer has been used for strings: the first character of the string will contain the pointer, the next character in the next address, etc. etc. until Reached Null-Terminal-Symbol \0 .

BUT: There is no need to do this in C ++. Use std :: string (or similar classes) instead. char * material has been named the single most commonly used source of security errors!

+15
source share

http://cplusplus.com/doc/tutorial/pointers/

The * symbol is displayed in two different places when working with pointers. First, the type "pointer to T" is denoted by T* (appending * to the type name). Secondly, when dereferencing a pointer, this is done by adding * to the name of the pointer variable that you want to dereference.

+1
source share

Space usually doesn't matter, so

 char* suchandsuch; char *suchandsuch; char * suchandsuch; 

all are the same.

+1
source share

All Articles