Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the difference between the

C++
char *arr[4] = {"C","C++","Java","VBA"};


and

C++
char arr[4] = {"C","C++","Java","VBA"};
Posted
Updated 16-Jul-15 23:11pm
v2

The first is an array of char pointers. So every member of the array is pointing to a char pointer (which often is a string).

char *arr[4];
arr[0]= "Codeproject";
arr[1]="is";
arr[2]="very";
arr[3]='"cool";


The other is only an array of chars. It only stores ONE char.
You can use it this way:

char arr[4];
arr[0]='C';
arr[1]='o';
arr[2]='o';
arr[3]='l';


Import is to understand that a pointer is ONLY pointing to memory or an object, but not allocating it. In this sample code the so called "hard coded strings" provide the memory.
 
Share this answer
 
Comments
Frankie-C 17-Jul-15 7:04am    
+5!
One is an array of 4 pointers to char, the other is a char array.
Multidimensional arrays have nothing to do with this case.
Leo Chapiro 17-Jul-15 7:19am    
FYI: an array of 4 pointers to char and two-dimentional array is the same thing.
Frankie-C 17-Jul-15 8:05am    
Thanks for the info, but after 30 years of programming and compiler construction technology I know something different.
You may want give a check to memory allocation using a debugger.
Leo Chapiro 17-Jul-15 8:52am    
Well, better to say: in this case by initialising of array of pointer with constant strings this is exactly the same like a two-dim array.
Frankie-C 17-Jul-15 9:45am    
Yes :), but only because the compiler by itself does the following:
1. Allocate 4 monodimensional char arrays and initialize them with the strings
2. Allocate an array of pointers and init it with the address of the 4 strings.
But it is *not* as a two dimensional array of chars, which layout in memory is the first array of chars, then the second, the third and the last one. And in the case of multidimensional arrays it is required that each array has the same dimension.
So if you have:
char *arr[4];
The memory layout is:
0023000 00231000 //address of string 1
0023004 00232000 //address of string 2
0023008 00450020 //address of string 3
002300c 00221344 //address of string 4

If you declare:
char arr[4][4] = {{'h', 'e', 'i', '\0'}, {'h', 'e', 'l', '\0'},{'h', 'a', 'i', '\0'},{'h', 'o', 'i', '\0'}};
and inspect the address of arr you'll find:
0023000 68 65 69 00 68 65 6c 00 68 61 69 00 68 6f 69 --- hei.hel.hai.hoi.
This is a C multidimensional array.
Try to compile this code and you don't need to ask:


C++
/* is OK, a two-dimentional array of char */
char *arr[4] = {"C","C++","Java","VBA"};



/* error C2040: 'char [4]' differs in levels of indirection from 'char *[4]' */	
char arr[4] = {"C","C++","Java","VBA"}; 


The error in the second case is that the array is one-dimentional and can take only 4 characters like:

C++
char arr1[4] = "C";
char arr2[4] = "C++";
char arr3[4] = "Java";
char arr4[4] = "VBA"; 
 
Share this answer
 
v4
Depending on the compiler you use, one of those won't compile.
The second version:
C++
char arr[4] = {"C","C++","Java","VBA"};
Declares arr to be an array of four characters, and then tries to supply it with a set of four pointer-to-character values.
Some compilers will complain "Too many initializers" because the first pointer alone provides all the data.
The first version:
C++
char *arr[4] = {"C","C++","Java","VBA"};
Declares arr to be an array of four pointer-to-character values, and allocates space for four pointers (i.e. probably either 4*4 bytes on a 32 bit system, or 8*4 bytes on a 64 bit system) on the stack.
The second version only ever allocates 4 bytes - one for each character - so you can't load it with "strings" without causing memory problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900