Need help declaring an array of strings 
Author Message
 Need help declaring an array of strings

I have a function that takes an array of pointers to strings:

#define NUM_COLS 80

void ttyShowMenu(char *tty_titles[NUM_COLS], unsigned num_titles);

I use this because it's easy for me to pass an array of strings, or
just one string.  The problem is when I want to define my array of
strings.  Currently, I have this:

char temp[4][NUM_COLS];
char *tty_titles[NUM_COLS] = {temp[0], temp[1], temp[2], temp[3]};

This is, unfortunately, clunky.  I was hoping to do something like
this:

char *tty_titles[NUM_COLS] = {char[NUM_COLS], char[NUM_COLS],
char[NUM_COLS], char[NUM_COLS]};

But of course, that's not legal.  Does anyone know how I clean this
up?



Sat, 05 Feb 2005 00:40:07 GMT  
 Need help declaring an array of strings

Quote:

>I have a function that takes an array of pointers to strings:
>#define NUM_COLS 80
>void ttyShowMenu(char *tty_titles[NUM_COLS], unsigned num_titles);

You are aware that tty_titles is a pointer to pointers to char, are you?
In a function parameter definition you can write char *tty_titles[],
or char **tty_titles, and there is no difference in meaning.

Quote:
>I use this because it's easy for me to pass an array of strings, or
>just one string.  The problem is when I want to define my array of
>strings.  Currently, I have this:
>char temp[4][NUM_COLS];

This is an array of 4 arrays of NUMCOLS chars.

Quote:
>char *tty_titles[NUM_COLS] = {temp[0], temp[1], temp[2], temp[3]};

Now, this is an array of NUM_COLS pointers to char, that decays to
a pointer to pointer to char in an expression context. You use
NUM_COLS for completely different things.

  char *tty_titles[] = { temp[0], temp[1], temp[2], temp[3] };

might be closer to what you want to express. Now you have an array
of 4 pointers to char, each pointing to the first of NUM_COLS consequitive
char elements, which seems to be closer to what you want.

Quote:
>This is, unfortunately, clunky.  I was hoping to do something like
>this:
>char *tty_titles[NUM_COLS] = {char[NUM_COLS], char[NUM_COLS],
>char[NUM_COLS], char[NUM_COLS]};
>But of course, that's not legal.  Does anyone know how I clean this
>up?

Did you have a look at Question 6.18 in the FAQ for this newsgroup?
I assume you want to pass tty_titles as the first parameter to
ttyShowMenu().

Kurt.
--
| Kurt Watzka                            



Sat, 05 Feb 2005 01:12:10 GMT  
 Need help declaring an array of strings

Quote:

> I have a function that takes an array of pointers to strings:

> #define NUM_COLS 80

> void ttyShowMenu(char *tty_titles[NUM_COLS], unsigned num_titles);

> I use this because it's easy for me to pass an array of strings, or
> just one string.  The problem is when I want to define my array of
> strings.  Currently, I have this:

> char temp[4][NUM_COLS];
> char *tty_titles[NUM_COLS] = {temp[0], temp[1], temp[2], temp[3]};

> This is, unfortunately, clunky.  I was hoping to do something like
> this:

> char *tty_titles[NUM_COLS] = {char[NUM_COLS], char[NUM_COLS],
> char[NUM_COLS], char[NUM_COLS]};

> But of course, that's not legal.  Does anyone know how I clean this
> up?

Sure, if your title arrays are always the same number of chars
(NUM_COLS) - it's just the *number* of titles that you pass to
ttyShowMenu that differs (and this looks like what you want), then
declare ttyShowMenu thus:

void ttyShowMenu(char (*tty_titles)[NUM_COLS], unsigned num_titles);

or equivalently:

void ttyShowMenu(char tty_titles[][NUM_COLS], unsigned num_titles);

tty_titles is now a pointer to array of NUM_COLS chars, and you may
happily pass it an array of array of NUM_COLS chars.

char temp[4][NUM_COLS];
/* stuff */
ttyShowMenu(temp, 4);

If you only want to pass in a single title:

char foo[NUM_COLS];
/* stuff */
ttyShowMenu(&foo, 1);

I think that's what you want.

        - Kevin.



Sat, 05 Feb 2005 14:26:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help - Array of pointers to strings dynamically declared

2. declaring array of strings - help!

3. URGENT HELP in declaring arrays of strings

4. Really Easy Help needed Managed c++ syntax for declaring array of unsigned long

5. HELP NEEDED to declare very large global array

6. Need help declaring an array.

7. Declaring/intialising two-dimentional array of character string variables

8. Declaring/intialising two-dimentional array of character string variables

9. how to declare 2D array of strings?

10. How do i declare string array?

11. how to declare array of string?

12. declaring arrays of strings

 

 
Powered by phpBB® Forum Software