Iseng
Iseng

Reputation: 17

Assign string to the pointer array

May I know why's the reason that when I declare a pointer array like

char *suits[]={"abc","cab","bca"}

there is no error even I am assigning a string instead of the address to pointer? Because from what I have learnt, the pointer can only be assigned by address(&) or another pointer(*). Even more puzzling part is when I print out the pointer array

cout<<suits[1]; 

I got the string "cab", instead of the address even I do not use the dereference operator. And when I use the dereference operator

cout<<*suits[1];

I got 'c' only.

To sum up the questions are 1)why I can assign a string to char type pointer instead of an address or pointer, and 2) why when I print out the char type pointer, instead of giving me the address, it shows out the string. 3)Finally,why is that when I dereference the char type pointer, it give me the value of the first char in the string.

Really wanna know why char type pointer is so different from others?

Upvotes: 0

Views: 1200

Answers (2)

You seem to be confused about what a string is in C.

A string is an array of chars, where the last char is '\0'.

If you write "abc" in your source code, anywhere, then the compiler will create an array for you, almost as if you had declared a global variable:

char abc_string[4] = {'a', 'b', 'c', '\0'};

(although it won't actually be called abc_string).

Same for "cab" and "bca".

Then, your definition is equivalent to this:

char *suits[]={abc_string, cab_string, bca_string};
// where abc_string, cab_string and bca_string are the arrays created by the compiler.

Now, when you use an array name in C, most of the time it is automatically converted (it "decays") to a pointer to its first element. So in the above definition, abc_string actually means &abc_string[0] - a pointer to the first element of abc_string.

char *suits[] = {&abc_string[0], &cab_string[0], &bca_string[0]};

Now it should be clear what the elements of suits point to - they point to the first character of each string.

This isn't specific to arrays - if you write:

char *thing = "hello world";

then the compiler generates an array containing {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'}, and sets thing to point to its first element.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

According to the C++ Standard

4.2 Array-to-pointer conversion [conv.array]

1 An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

In this declaration

char *suits[]={"abc","cab","bca"}

all string literals have type const char[4] and initializer expressions based on these string literals are converted to pointers to first elements of the string literals. In C++ they have type const char *. Thus it would be more correctly to declare the array like

const char *suits[]={"abc","cab","bca"}

For pointers to char operator << is explicitly overloaded and performs output characters pointed to by the pointer until the zero-terminating character will be encountered. That is it is supposed that if you use a pointer to char in operator << then you are going to output a string pointed to by this pointer.

As for this record

cout<<*suits[1];

then according to the definition of suits as an array of pointers suits[1] is the second element of the array and it is the pointer that points to the first element of string literal "cab". So dereferencing the pointer you will get the first element of the string literal that is character 'c'.

Upvotes: 2

Related Questions