| | |
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:
Solved Threads: 0
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.:
Why doesn't the following work, and instead give a series of "invalid conversion from `const char*' to `char'" errors?:
Why are char arrays different than say int 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)
char * carr = "Hello";
C++ Syntax (Toggle Plain Text)
char carr[5] = {"H", "e", "l", "l", "o"};
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
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.:
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)
char * carr = "Hello";
Why are char arrays different than say int arrays?C++ Syntax (Toggle Plain Text)
char carr[5] = {"H", "e", "l", "l", "o"};
C++ Syntax (Toggle Plain Text)
char carr[5] = {"H", "e", "l", "l", "o"};
C++ Syntax (Toggle Plain Text)
char carr[5] = {'H', 'e', 'l', 'l', 'o'};
Some arrays...
A char array (and only char arrays), can also be initialised like this (for convenience)
Finally, also for char only, is a pointer to a string constant
A pointer to a string constant is like this to the compiler. 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.
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
const char anonymous[ ] = "123"; char *arr = anonymous;
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.
•
•
Join Date: Mar 2008
Posts: 1,412
Reputation:
Solved Threads: 114
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
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 :)
•
•
Join Date: Dec 2004
Posts: 42
Reputation:
Solved Threads: 0
•
•
•
•
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.
Is there any programming reason why I always see function definitions with arguments like this:
C++ Syntax (Toggle Plain Text)
char getchar(char *arr){
C++ Syntax (Toggle Plain Text)
char getchar(char arr[]){
C++ Syntax (Toggle Plain Text)
const char anonymous[ ] = "123"; char *arr = anonymous;
C++ Syntax (Toggle Plain Text)
char anonymous[ ] = "123"; char *arr = anonymous;
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
•
•
•
•
Is there any programming reason why I always see function definitions with arguments like this:
Instead of this?:C++ Syntax (Toggle Plain Text)
char getchar(char *arr){
C++ Syntax (Toggle Plain Text)
char getchar(char arr[]){
•
•
•
•
Also this code:
Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.C++ Syntax (Toggle Plain Text)
const char anonymous[ ] = "123"; char *arr = anonymous;
•
•
•
•
Would it be correct to say that char *arr = "123"; is like this to the compiler:
Without the constant declaration?C++ Syntax (Toggle Plain Text)
char anonymous[ ] = "123"; char *arr = anonymous;
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
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
For the moment, you can get away with it, but you should really start using const to be future-proof.
> 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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: tracing the SEMANTICS
- Next Thread: Poker Game? ..Questions
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






