943,777 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3524
  • C++ RSS
May 28th, 2008
0

Confused about use * in declaring char arrays

Expand Post »
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.:
C++ Syntax (Toggle Plain Text)
  1. char * carr = "Hello";
Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:
C++ Syntax (Toggle Plain Text)
  1. char carr[5] = {"H", "e", "l", "l", "o"};
Why are char arrays different than say int arrays?
Reputation Points: 10
Solved Threads: 0
Light Poster
Kadence is offline Offline
46 posts
since Dec 2004
May 28th, 2008
0

Re: Confused about use * in declaring char arrays

Click to Expand / Collapse  Quote originally posted by Kadence ...
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.:
C++ Syntax (Toggle Plain Text)
  1. char * carr = "Hello";
Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:
C++ Syntax (Toggle Plain Text)
  1. 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:
C++ Syntax (Toggle Plain Text)
  1. char carr[5] = {"H", "e", "l", "l", "o"};
to
C++ Syntax (Toggle Plain Text)
  1. char carr[5] = {'H', 'e', 'l', 'l', 'o'};
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
May 28th, 2008
0

Re: Confused about use * in declaring char arrays

Ah I see, yes that works.

However I still don't understand the use of the * operator in the context of declaring arrays.
Reputation Points: 10
Solved Threads: 0
Light Poster
Kadence is offline Offline
46 posts
since Dec 2004
May 29th, 2008
0

Re: Confused about use * in declaring char arrays

Some arrays...
C++ Syntax (Toggle Plain Text)
  1. int arr[ ] = { 1, 2, 3 };
  2. double arr[ ] = { 1.0, 2.0, 3.0 };
  3. 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.
C++ Syntax (Toggle Plain Text)
  1. const char anonymous[ ] = "123";
  2. 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 29th, 2008
0

Re: Confused about use * in declaring char arrays

I think you need to learn how to use pointers. Heres a tutorial:
You probably only need to look at the Pointer initialization section.
http://www.cplusplus.com/doc/tutorial/pointers.html
Last edited by William Hemsworth; May 29th, 2008 at 8:10 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 29th, 2008
0

Re: Confused about use * in declaring char arrays

Click to Expand / Collapse  Quote originally posted by Salem ...
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:
C++ Syntax (Toggle Plain Text)
  1. char getchar(char *arr){
Instead of this?:
C++ Syntax (Toggle Plain Text)
  1. char getchar(char arr[]){
Also this code:
C++ Syntax (Toggle Plain Text)
  1. const char anonymous[ ] = "123";
  2. 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:
C++ Syntax (Toggle Plain Text)
  1. char anonymous[ ] = "123";
  2. char *arr = anonymous;
Without the constant declaration?
Reputation Points: 10
Solved Threads: 0
Light Poster
Kadence is offline Offline
46 posts
since Dec 2004
May 29th, 2008
1

Re: Confused about use * in declaring char arrays

Click to Expand / Collapse  Quote originally posted by Kadence ...
Is there any programming reason why I always see function definitions with arguments like this:
C++ Syntax (Toggle Plain Text)
  1. char getchar(char *arr){
Instead of this?:
C++ Syntax (Toggle Plain Text)
  1. 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.

Click to Expand / Collapse  Quote originally posted by Kadence ...
Also this code:
C++ Syntax (Toggle Plain Text)
  1. const char anonymous[ ] = "123";
  2. 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 []".

Click to Expand / Collapse  Quote originally posted by Kadence ...
Would it be correct to say that char *arr = "123"; is like this to the compiler:
C++ Syntax (Toggle Plain Text)
  1. char anonymous[ ] = "123";
  2. 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.
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
May 30th, 2008
0

Re: Confused about use * in declaring char arrays

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: tracing the SEMANTICS
Next Thread in C++ Forum Timeline: Poker Game? ..Questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC