array of array of pointers

P

Piotrek

Hi there again!
Last time you helped me with pointers - it let me to save many hours of
searching for some solutions. And once again I have question.
Let's declare array of pointers:

char *O={"", "one", "two",..., "eighteen"};
char *T={"", "ten", "twenty",..., "eighty"};

And now I want to declare an array of arrays of pointers:
something like:

char A={O,T};

Compiler says: "error C2440: 'initializing' : cannot convert from 'char
*[10]' to 'char'".
Here we go with first question: How to declare such array?

The point is that I want to refer to ie. "one" (O[1]) by something like
A[0][1];
That's my second question: How to refer to "one" by array A?

I hope you understand what I mean ... :)

Thank you in advance!!!


BTW
I couldn't find it in FAQ
And, please, feel free to correct my English!
 
B

Bill Pursell

Last time you helped me with pointers - it let me to save many hours of
searching for some solutions. And once again I have question.
Let's declare array of pointers:

char *O={"", "one", "two",..., "eighteen"};
char *T={"", "ten", "twenty",..., "eighty"};

This is a compile time error. {"", "one", ...}
is an array of char *, so the type should be
char *O[];
And now I want to declare an array of arrays of pointers:
something like:

char A={O,T};
How to declare such array?

Something like this...

#include <stdlib.h>
#include <stdio.h>

int
main( void )
{
char *O[] = {"", "one", "two", "eighteen"};
char *T[] = {"", "ten", "twenty", "eighty"};
char **A[2];
A[0] = O;
A[1] = T;

printf( "%s\n", A[0][2]) ;
return EXIT_SUCCESS;
}
 
E

Eric Sosman

Piotrek said:
Hi there again!
Last time you helped me with pointers - it let me to save many hours of
searching for some solutions. And once again I have question.
Let's declare array of pointers:

char *O={"", "one", "two",..., "eighteen"};
char *T={"", "ten", "twenty",..., "eighty"};

Is this what you actually wrote? The compiler should
have complained about it, because the initializers do not
match the things being initialized. I'll assume you wrote
something more like

char *O[] = { "", "one", ... };

.... which agrees better with the initializer and with the
samples you show below.
And now I want to declare an array of arrays of pointers:
something like:

char A={O,T};

Compiler says: "error C2440: 'initializing' : cannot convert from 'char
*[10]' to 'char'".
Here we go with first question: How to declare such array?

One way is

char **A[] = { O, T };
The point is that I want to refer to ie. "one" (O[1]) by something like
A[0][1];
That's my second question: How to refer to "one" by array A?
[...]
BTW
I couldn't find it in FAQ

Thanks for checking. Even though it's not exactly what
you're looking for, Question 1.21 may be helpful.
 
P

Piotrek

[...]
char A={O,T};
[...]

I mean char A[]={O,T};

Or maybe I should declare array of pointers to array of pointers, but it
still fails to compile:
char *A[]={O,T};
 
P

Piotrek

Eric said:
[...]
char *O={"", "one", "two",..., "eighteen"};
char *T={"", "ten", "twenty",..., "eighty"};

Is this what you actually wrote? The compiler should
have complained about it, because the initializers do not
match the things being initialized. I'll assume you wrote
something more like

char *O[] = { "", "one", ... };

Of course I wrote char *O[]=... I made mistake writing a post. I see you
read very carefully - thanks!

[...]
One way is

char **A[] = { O, T };
The point is that I want to refer to ie. "one" (O[1]) by something
like A[0][1];
[...]
Thanks for checking. Even though it's not exactly what
you're looking for, Question 1.21 may be helpful.

This is EXACTLY what I'm looking for, thanks a lot!
 
E

Eric Sosman

Piotrek said:
[...]
Of course I wrote char *O[]=... I made mistake writing a post. I see you
read very carefully - thanks!

<off-topic>

General observation, not specific to C: A programmer either
develops an eye for detail, or develops a different career.

Computers are very obedient, and have a nasty tendency to do
what you tell them instead of what you meant to tell them.

</off-topic>
 
D

Default User

Piotrek wrote:

Of course I wrote char *O[]=... I made mistake writing a post. I see
you read very carefully - thanks!


That's exactly why we request that you cut and past the EXACT program
that you have. Not something like it. Not the code retyped. No missing
pieces. No ... to show stuff left out.

The. Exact. Program.


Learn that. Embrace that. Live that.






Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top