Confused about use * in declaring char arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2004
Posts: 42
Reputation: Kadence is an unknown quantity at this point 
Solved Threads: 0
Kadence Kadence is offline Offline
Light Poster

Confused about use * in declaring char arrays

 
0
  #1
May 28th, 2008
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.:
  1. char * carr = "Hello";
Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:
  1. char carr[5] = {"H", "e", "l", "l", "o"};
Why are char arrays different than say int arrays?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Confused about use * in declaring char arrays

 
0
  #2
May 28th, 2008
Originally Posted by Kadence View 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.:
  1. char * carr = "Hello";
Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:
  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:
  1. char carr[5] = {"H", "e", "l", "l", "o"};
to
  1. char carr[5] = {'H', 'e', 'l', 'l', 'o'};
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 42
Reputation: Kadence is an unknown quantity at this point 
Solved Threads: 0
Kadence Kadence is offline Offline
Light Poster

Re: Confused about use * in declaring char arrays

 
0
  #3
May 28th, 2008
Ah I see, yes that works.

However I still don't understand the use of the * operator in the context of declaring arrays.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Confused about use * in declaring char arrays

 
0
  #4
May 29th, 2008
Some arrays...
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,412
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Confused about use * in declaring char arrays

 
0
  #5
May 29th, 2008
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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 42
Reputation: Kadence is an unknown quantity at this point 
Solved Threads: 0
Kadence Kadence is offline Offline
Light Poster

Re: Confused about use * in declaring char arrays

 
0
  #6
May 29th, 2008
Originally Posted by Salem View Post
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:
  1. char getchar(char *arr){
Instead of this?:
  1. char getchar(char arr[]){
Also this code:
  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:
  1. char anonymous[ ] = "123";
  2. char *arr = anonymous;
Without the constant declaration?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Confused about use * in declaring char arrays

 
1
  #7
May 29th, 2008
Originally Posted by Kadence View Post
Is there any programming reason why I always see function definitions with arguments like this:
  1. char getchar(char *arr){
Instead of this?:
  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.

Originally Posted by Kadence View Post
Also this code:
  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 []".

Originally Posted by Kadence View Post
Would it be correct to say that char *arr = "123"; is like this to the compiler:
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Confused about use * in declaring char arrays

 
0
  #8
May 30th, 2008
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC