Array vs Element

What is the difference (according to my C++ compiler) between
cout << cnArray;
and
cout << cnArray[0];

assuming that cnArray is declared and defined as
char* cnArray[80]={"hello world"}

When you dereference "cnArray" you get the address of the character located in the 0th element of the array. So then should not cnArray[0] and cnArray be pointing to the same space (assuming the brackets [] did not dereference automatically), therefor printing the same thing when using the cout statement?
Fixing the obvious error, your declaration is
const char* cnArray[80]={"hello world"};
which defines an array of 80 pointers to const char, sets the first one to point at the first character of the string literal "hello world" and sets the remaining 79 of them to null.

The line
cout << cnArray[0];
takes the first element of cnArray, which is a const char* pointer to the first element of a string literal, and engages the operator<< for C strings: it prints every character in the pointed-to array until the null terminator.

The line
cout << cnArray;
takes the array and converts it to a pointer to its first element, that is, a new nameless const char**, which is pointing to cnArray[0], which is pointing to 'h'. Since this is not a pointer to a char, this engages the operator<< for pointers, which prints the memory address stored in the pointer.

In other words,
cout << cnArray[0]; is the same as cout << *cnArray;
and
cout << cnArray; is the same as cout << &cnArray[0];



My 'obvious error' was due to my poorly simplifying a larger project so I could get at the crux of the matter.

A constant character array is useless to me. I'd need to be able to change it based on user input. Nowhere in my code do I have a string literal that is "hello world." In fact, nowhere in my code do I have a string literal. All character arrays in my code are set based on user input. What can I do to fix this?

As far as I can tell (I typically can't tell very much), this isn't a problem with dynamically allocating memory. Just so I can help those who are attempting to help me.
My 'obvious error' was due to my poorly simplifying a larger project so I could get at the crux of the matter.

Did you read beyond the first paragraph? Or did you just see "obvious error" and assume the rest didn't apply? If you want your question answered in a different context, provide the context. If you have a different question, ask that question.


What can I do to fix this?

What is this?
I did indeed read beyond the first paragraph, which is why I didn't pursue my initial question further - it had been answered by the succeeding paragraphs written by Cubbi.

However, the 1st paragraph he/she wrote created an other question of mine. He pointed out that when declaring an array the way I declared it in the context that I gave (where the string is given in the declaration), it needs the const keyword. In my code, I don't use the const modifier because I don't define cnArray when I declare it (or, I do define it, but by setting all of the elements to null characters). I put the "hello world" string literal in the context of my question in order to highlight my question and eliminate as much extraneous detail as possible.
this is the problem I described in my 1st reply.

I'd need to be able to change it based on user input. Nowhere in my code do I have a string literal that is "hello world." In fact, nowhere in my code do I have a string literal. All character arrays in my code are set based on user input.

How can I do this if the elements of an array must be declared as const, as Cubbi pointed out. Which happened to be the 1st paragraph that he wrote.
The const requirement derived from the fact that you were pointing to a string literal (since you cannot modify a string literal.) You aren't doing this in the "real" code, so there is no such requirement and there is no problem to fix.
There is some confusion because the original post contained example code which you go on to explain is not the actual code which you are using. e.g. 'Nowhere in my code do I have a string literal that is "hello world."'

There is more confusion as in both the original post and the later one, you refer to the individual elements of the array as though they were characters, rather than pointers.

That leaves me wondering whether the actual code, rather than looking like this:
char* cnArray[80];
might look like this
char cnArray[80];

I don't know which one applies but the verbal description hints at the second. Could you please clarify by giving the specific code you use.
Well, what IS the difference between
char* cnArray[80]
and
char cnArray[80]?

My program is collectively about 230 lines long, with 3 or 4 various header files. I'm aware that newbies who insist on ambiguity are the scourge of forums like these, but I'm trying to hold off on posting pages of code unless it's absolutely necessary.

For the record, I have gotten my program to function correctly even with my lacking knowledge of arrays (which I am obviously using quite frequently). I keep a project that is essentially a blank slate open where I will spend 5 minutes typing in simplistic code to test certain postulates of mine about arrays because I haven't been able to correctly understand them when I read about them.

Edits begin here.
Fuck it. I don't want to aggravate the people who make this community function (lol. developer pun.) Lets do this.

1
2
const int MAX=80;
	char cnArray[MAX];

^declared in a namespace residing in a header file that is #included in main.cpp

GetInput();
^the 1st line of void main(){...

1
2
3
4
5
void GetInput(){                         //this will get input from the command line and remove the spaces from it.
		std::cout <<"Enter problem." << std::endl;
		std::cin.getline(cnArray,MAX);
		RemoveSpace(cnArray);
		return;}

^fx declared in the same namespace as the 1st piece of code in this post.

1
2
3
4
5
6
7
8
9
10
11
12
13
void RemoveSpace(char* nospacesArray){
		for(counterA=0,counterB=0,counterC=1;counterA!=MAX;counterA++,counterB=counterA,counterC=counterB+1){
			if(int(nospacesArray[counterA])==32){
				while(counterB!=MAX){
					nospacesArray[counterB]=nospacesArray[counterC];
					counterB++,counterC++;}
				counterB=counterA;
				counterC=counterB+1;
				counterA--;}
			else
				continue;}
		return;
	}

^fx declared in the same namespace as the 1st piece of code in this post.

I've tested my code and it has worked. But in the future I can certainly see how my roundabout bootleg way of working with arrays might be my doom.


Edit #2. Don't poke fun at my pitifully excessive function to remove the spaces from a c-string... I did it all by myself from scratch because, when I looked it up, I couldn't understand why other peoples functions, designed to do the same thing, worked. And I don't generally like using things if I don't fully understand them. I'm sure the universe is getting a kick out of the irony of this thread.
Last edited on
char* cnArray[80]; is an array holding 80 pointers to char.
char cnArray[80]; is an array holding 80 chars.

Why do you try to work with arrays anyway? Based on what you're showing, this task appears to be something best handled by strings, as in,

string cnArray;
Last edited on
One is a character string (or an array of characters), the other is an array of pointers to character strings.

1
2
3
    char cnArray[80] = "hello world";
    cout << cnArray << endl;
    cout << cnArray[0] << endl;

Output:
hello world
h
First example above, the output is the entire null-terminated string, followed by the first character of that string.



1
2
3
    const char * cnArray[80] = {"hello world"};
    cout << cnArray << endl;
    cout << cnArray[0] << endl;

Output:
13fe28
hello world
Second example above, output is a pointer to the start of the array, followed by the string pointed to by the first element of the array.
Last edited on
I am trying to work with arrays because if I take the time to master arrays, then working with the string class in the iostream library will be a piece of cake. And thank you, Cubbi, Chervil. I think I get it now.

What would be the use of an array of pointers all pointing to the 0th element of a character array? I'm just curious.
I think it's counterproductive (see http://cplusplus.com/forum/lounge/88581/ for some of the reasons why)

an array of pointers all pointing to the same element of some other array? I can't think of a use either. (note that cnArray in the post above isn't that, though, it holds 80 pointers, but only the first one is pointing at 'h' of the string)
Last edited on
This is perhaps a more realistic example, though std::string etc might be simpler to use and understand.
1
2
3
4
5
    const char * cnArray[7] = {"red", "yellow", "green", "brown",
                                "blue", "pink", "black"};
    cout << cnArray[0] << endl;
    cout << cnArray[2] << endl;
    cout << cnArray[6] << endl;

Output:
red
green
black

Last edited on
Topic archived. No new replies allowed.