I'm confused about the use of the dereference operator in the declaring of char arrays.

I think I understand that the dereference operator is used to dereference memory addresses to their values, and also in an unrelated function in the declaration of pointers; and that arrays are like special pointers to memory addresses.

But why is the * operator used in declaring char arrays, e.g.:

char * carr = "Hello";

Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:

char carr[5] = {"H", "e", "l", "l", "o"};

Why are char arrays different than say int arrays?

Recommended Answers

All 7 Replies

I'm confused about the use of the dereference operator in the declaring of char arrays.

I think I understand that the dereference operator is used to dereference memory addresses to their values, and also in an unrelated function in the declaration of pointers; and that arrays are like special pointers to memory addresses.

But why is the * operator used in declaring char arrays, e.g.:

char * carr = "Hello";

Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:

char carr[5] = {"H", "e", "l", "l", "o"};

Why are char arrays different than say int arrays?

Double quotes represent strings, single quotes represent chars. Try changing the below code from double quotes to single:

char carr[5] = {"H", "e", "l", "l", "o"};

to

char carr[5] = {'H', 'e', 'l', 'l', 'o'};

Ah I see, yes that works.

However I still don't understand the use of the * operator in the context of declaring arrays.

Some arrays...

int arr[ ] = { 1, 2, 3 };
double arr[ ] = { 1.0, 2.0, 3.0 };
char arr[ ] = { '1', '2', '3' };

A char array (and only char arrays), can also be initialised like this (for convenience) char arr[ ] = "123"; Finally, also for char only, is a pointer to a string constant char *arr = "123"; A pointer to a string constant is like this to the compiler.

const char anonymous[ ] = "123";
char *arr = anonymous;

the only difference being is that you never get to see the anonymous name the compiler generates.
Again, this is a programmer convenience which only applies to char.


If you wanted say a pointer to an integer constant, then you would have to do that the long way by yourself.

A char array (and only char arrays), can also be initialised like this (for convenience) char arr[ ] = "123"; Finally, also for char only, is a pointer to a string constant char *arr = "123"; A pointer to a string constant is like this to the compiler.

Thank you, so this is just a special shorthand for char arrays.

Is there any programming reason why I always see function definitions with arguments like this:

char getchar(char *arr){

Instead of this?:

char getchar(char arr[]){

Also this code:

const char anonymous[ ] = "123";
char *arr = anonymous;

Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error. Would it be correct to say that char *arr = "123"; is like this to the compiler:

char anonymous[ ] = "123";      
char *arr = anonymous;

Without the constant declaration?

Is there any programming reason why I always see function definitions with arguments like this:

char getchar(char *arr){

Instead of this?:

char getchar(char arr[]){

These are identical. It is a pointer in both cases. So I guess it may be more clear to write it as a pointer. The second case might cause beginners to think that you are passing an entire array by value or something like that.

Also this code:

const char anonymous[ ] = "123";
char *arr = anonymous;

Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.

Yes. Either "arr" needs be changed to be declared as "const char *", or "anonymous" needs be changed to be declared as "char []".

Would it be correct to say that char *arr = "123"; is like this to the compiler:

char anonymous[ ] = "123";      
char *arr = anonymous;

Without the constant declaration?

No. There are two special syntaxes here. But they are very different. The line 'char *arr = "123" ' makes a string literal (which is stored specially in some special part of memory by the compiler, and exists for the entire duration of the program) and gives the location of that to the pointer. The second code example creates an array which is a local variable of the current function, and initializes it. As a local variable, it only exists until the function returns, so it would be bad to return a pointer to it or refer it somewhere else.

By the way, when you use string literals, you should always make it "const char *", even though the compiler doesn't force you to make it const. This is because they are usually stored in a read-only part of memory.

> Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.
> Would it be correct to say that char *arr = "123"; is like this to the compiler:
The ability to do char *arr = "a string"; is a special case relaxation of the rules. In other situations, the compiler will tell you whether you're breaking 'const-correctness'.

Some time ago, a change was added to the standards to allow"string" constants to be placed in read-only memory (and thus const). But they don't have to be. Another thing the standards people try to do is avoid breaking existing common practice in existing code.

Any new code, and any code being maintained should be writing const char *arr = "a string"; For the moment, you can get away with it, but you should really start using const to be future-proof.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.