Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
fc34
Minister of Gerbil Affairs
Topic Author
Posts: 2816
Joined: Wed May 08, 2002 8:39 am
Location: Somewhere

Array question in c++

Thu Sep 18, 2003 9:18 am

Just wondering, say i want to have an array of character arrays

why wont this work

char **a = {"string1", "string2", "string3"}

since it works in the main function

int main(int argc, char **argv)
Windows XP - The 64-bit wannabe with a 32-bit graphics interface for 16-bit extensions to a 8-bit patch on a 4-bit operating system designed to run on a 2-bit processor by a company that can't stand 1-bit of competition
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

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.
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Thu Sep 18, 2003 6:01 pm

char **a defines a pointer to a char *. char *a[] defines an array of char pointers. The actualy definition for a data structure like the arguments for main() is:

char *a[] = {"string1", "string2", "string3"};

--SecretSquirrel
 
Craig P.
Gerbil Team Leader
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Thu Sep 18, 2003 6:14 pm

While we're recommending books, Accelerated C++ by Koenig and Moo is very highly recommended, if you already have some familiarity with programming.

ACCU book reviews - beginner's C++ is an excellent resource for book reviews.

C++ Primer 3rd Ed by Lippman and Lajoie (suggested by Buub) is also a highly recommended book by the ACCU.
 
fc34
Minister of Gerbil Affairs
Topic Author
Posts: 2816
Joined: Wed May 08, 2002 8:39 am
Location: Somewhere

Sat Sep 20, 2003 8:31 am

Thanks for ur help guys!
Windows XP - The 64-bit wannabe with a 32-bit graphics interface for 16-bit extensions to a 8-bit patch on a 4-bit operating system designed to run on a 2-bit processor by a company that can't stand 1-bit of competition

Who is online

Users browsing this forum: No registered users and 14 guests
GZIP: On