Difference between char r[] = "red"; and char *q = "Quit";

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main(){
	char r[] = "red";
	char *q = "Quit";
	if (r<q)
	cout << r << endl << q << endl;
	return 0;
}


Output:
red
Quit
the bracket can either contain a value corresponding to the number of elements contained the in the array, or you can have nothing inside the bracket but this means you have to have it assigned to some value that the compiler can see. And in this case, that value is the string "red". If you have just the bracket and no numbers inside it and it is not assigned to a value, you will receive a compiler error. The pointer 'q' is pointing to a memory space containing the string "Quit". Since pointers can only do one thing which is to 'point', you can access the value q is pointing to. But if you just specify q and not initialise it using the new keyword, and try to access the value of q, you will get a compiler error.

Both, are examples of pointers, just with different syntax.
I got every bit of what you said. Another question: in r[] = "red", r is a pointer pointing to a memory space containing string "red"?

And what is being compared (r<q) and (r>q) in both cases condition is TRUE, why?
Last edited on
#>in r[] = "red", r is a pointer pointing to a memory space containing string "red"

Yes

#>And what is being compared (r<q) and (r>q) in both cases condition is TRUE, why?


I believe their lexicographic order is what is being compared

Example code in python

1
2
3
4
r = str(raw_input())
q = str(raw_input())
print q > r
print q < r


red
Quit
False
True


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
	char r[] = "red";
	char *q = "Quit";
	if (r<q)
		cout << r << endl << q << endl;
	cout << ((r > q) ? "True\n" : (r < q) ? "False\n" : "");
	return 0;
}


True
Last edited on
I understood everything except:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main(){
	char r[] = "red";
	char *q = "quit";
	if (r<q)
	cout << r << endl << q << endl;

	strcpy(r,"quit");
	q = "red";
	if (r<q)
	cout << r << endl << q << endl;

	return 0;
}


why condition is true in both cases. Where is lexicographic order?

Output:
red
quit
quit
red
Last edited on
Array of char
 
char r[] = "red";


This should be forbidden.
 
char *q = "quit";

Pointer that points to a temporary object.
if (r<q) is comparing the memory addresses of r and q. When you copy "quit" into the array which contains "red" to start with, the contents of the array is changed but not its location in memory.

Difference between char r[] = "red"; and char *q = "Quit";


char r[] = "red";

allocates a char array of 4 chars on the stack which contains "red" plus a null terminator (see note [1] below), whereas

char *q = "Quit";

allocates a char* variable, on the stack, which points to a string ("quit") in the const segment of your executable. This is readonly data, so your program will die if you try to change an element of the string that q points to [2].

Note that this line

char* q = "red";

does not cause q to point to a temporary: it causes q to point at a const string in the const segment of your execuable (as I said above).

Andy

Notes:

[1] Careful! The strcpy on line 11 is copying 5 chars (quit + null terminator) into an array variable (r) which is only 4 chars (or elements) long.

As you didn't specify a length, r will be allocated (on the stack) to be just big enough to store the string "red" plus null terminator, which is 4 chars.

So your copy is causing stack damage!

To avoid this problem, you should specify a length for your array which makes sure there's enough room to work with later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
	char r[256] = "red"; // all elements after the d will be zeroed for you
	cout << r << endl;

	strcpy(r, "quit");
	cout << r << endl;

        strcat(r, " and yellow and green and blue");
	cout << r << endl;

	return 0;
}


[2] In C++ code you should only ever use const char* to point to string literals, so the compiler stops you making the mistake of changing an element. (the use of char* in this situation is only allowed to provide backwards compatiblity with old versions of C)
Last edited on
Thanks everybody. And special thanks to andywestken and smac.
@andywestken
As you didn't specify a length, r will be allocated (on the stack) to be just big enough to store the string "red" plus null terminator, which is 4 chars.


r is being allocated on stack because of not specifying the length char r[] = "--"; or because of exceeding length while copying?
If you declare a local array variable it will always be allocated on the stack. The only dfference is whether you specify the length or the compiler calculates it for you.

Andy

PS One thing I forgot earlier:

the reason if (r<q) evaluates true both times is because q is modified to point at the string literal "red" which is stored in the const segment (near to the literal "quit"), which is in a different place (in memory) to the stack, where the array r is located.
Last edited on
Topic archived. No new replies allowed.