Thu Sep 18, 2003 5:43 pm
fc34, have you ever thought about taking a C++ class? Or just getting a good book on it, like The C++ Primer, by Lippman I believe? You really need to start taking responsibility for educating yourself if you're serious about working with C++.
First, {"string1", "string2", "string3"} isn't a pointer to strings. It's a collection (basically a struct) of strings or char pointers which can be interpreted by the compiler in multiple ways.
Basically your literal definition might say something like this:
struct {
const char* s1;
const char* s2;
const char* s3;
} a = {"string1", "string2", "string3"};
Second, you have const chars in your strings, not chars. You need a "const char*".
Now, think on this simpler example:
int i[] = { 1, 2, 3 };
sizeof(i) will be 12 (four-byte ints times 3).
You're declaring a const-pointer fixed-sized array, then telling it where to find the const contents to fill it. That is why this won't work:
int j[];
It's const and pre-sized, but it has no contents assigned to it, meaning it doesn't know the size and it has no const value to assign.
Now extrapolate that back to your example. You're trying to shove that in a**, but that says pointer to pointer to char. You aren't assigning it a pointer, you're assigning it a constant fixed sized array.
So what you are trying to do is to declare a const array of pointers to const char:
const char* a[] = { "string1", "string2", "string3" };
We're taking that collection of pointers to const char pointers and sticking them into a const array (fixed size array).
You really need to build a little test program and experiment yourself to figure this stuff out. If I'm unsure of how something works I always write a small program and experiment to see how different things I try actually work. This should get you started. Add to it and experiment:
int main(int argc, char* argv[])
{
printf("Hello World!\n");
struct {
const char* s1;
const char* s2;
const char* s3;
} s = {"string1", "string2", "string3"};
const char* a[] = { "string1", "string2", "string3" };
printf("s1: %s, s2: %s, s3: %s\n", a[0], a[1], a[2]);
const char* b[] = { &"string1", &"string2", &"string3" };
printf("s1: %s, s2: %s, s3: %s\n", b[0], b[1], b[2]);
const char c[][] = { "string1", "string2", "string3" };
printf("s1: %s, s2: %s, s3: %s\n", c[0], c[1], c[2]);
const char d[][] = { &"string1", &"string2", &"string3" };
printf("s1: %s, s2: %s, s3: %s\n", d[0], d[1], d[2]);
int i[] = { 1, 2, 3 };
printf("sizeof i: %d\n", sizeof(i));
//int j[];
//int* k = { 1, 2, 3 };
return 0;
}
What is really kind of interesting is the error messages for c and d:
test.c:29: declaration of `c' as multidimensional array
test.c:29: must have bounds for all dimensions except the first
test.c:29: assignment (not initialization) in declaration
test.c:30: `c' undeclared (first use this function)
test.c:30: (Each undeclared identifier is reported only once
test.c:30: for each function it appears in.)
test.c:32: declaration of `d' as multidimensional array
test.c:32: must have bounds for all dimensions except the first
test.c:32: assignment (not initialization) in declaration
test.c:33: `d' undeclared (first use this function)
Last edited by
Buub on Sat Sep 20, 2003 8:50 am, edited 2 times in total.