char array (array of array...) question 
Author Message
 char array (array of array...) question

I have the following code:

static char* flintstones[] =
{
    "fred",
    "wilma"

Quote:
};

static char* jetsons[] =
{
   "george",
   "judy"

Quote:
};

and I want to declare another array:

//I'm not sure how to do the following here...
static char* cartoons[] =
{
  flintstones,
  jetsons

Quote:
}

Therefore,
cartoons[0][1] would be "fred"

My question is how do I define the last array?  I think I'm close, but
just missing something small.

Any help is appreciated.

Coleman



Tue, 19 Apr 2005 12:52:29 GMT  
 char array (array of array...) question


Quote:
> I have the following code:

> static char* flintstones[] =
> {
>     "fred",
>     "wilma"
> };

> static char* jetsons[] =
> {
>    "george",
>    "judy"
> };

> and I want to declare another array:

> file://I'm not sure how to do the following here...
> static char* cartoons[] =
> {
>   flintstones,
>   jetsons
> }

> Therefore,
> cartoons[0][1] would be "fred"

cartoons[0][1] can only be a char, not a char*.

- Show quoted text -

Quote:
> My question is how do I define the last array?  I think I'm close, but
> just missing something small.

> Any help is appreciated.

> Coleman



Tue, 19 Apr 2005 14:53:10 GMT  
 char array (array of array...) question

Quote:
> I have the following code:

> static char* flintstones[] =
> {
>     "fred",
>     "wilma"
> };

> static char* jetsons[] =
> {
>    "george",
>    "judy"
> };

> and I want to declare another array:

> //I'm not sure how to do the following here...
> static char* cartoons[] =
> {
>   flintstones,
>   jetsons
> }

The type of 'flintstones' and 'jetsons' is char**, hence you want an array
of char **:

static char** cartoons[] =
{
   flintstones,
   jetsons

Quote:
};

#include <stdio.h>

int main (void)
{
   static char *flintstones[] =
   {
      "fred",
      "wilma"
   };

   static char *jetsons[] =
   {
      "george",
      "judy"
   };

   static char **cartoons[] =
   {
      flintstones,
      jetsons
   };

   printf ("%s\n", cartoons[0][0]);
   printf ("%s\n", cartoons[0][1]);
   printf ("%s\n", cartoons[1][0]);
   printf ("%s\n", cartoons[1][1]);

   return 0;

Quote:
}

C:\CLC\B\BRUMELY>bc
fred
wilma
george
judy

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.



Tue, 19 Apr 2005 16:34:57 GMT  
 char array (array of array...) question

Quote:



> > I have the following code:

> > static char* flintstones[] =
> > {
> >     "fred",
> >     "wilma"
> > };

> > static char* jetsons[] =
> > {
> >    "george",
> >    "judy"
> > };

> > and I want to declare another array:

> > file://I'm not sure how to do the following here...
> > static char* cartoons[] =
> > {
> >   flintstones,
> >   jetsons
> > }

> > Therefore,
> > cartoons[0][1] would be "fred"

> cartoons[0][1] can only be a char, not a char*.

> > My question is how do I define the last array?  I think I'm close, but
> > just missing something small.

> > Any help is appreciated.

> > Coleman

So, how do I define "cartoons"?


Tue, 19 Apr 2005 21:18:58 GMT  
 char array (array of array...) question

Quote:


> > I have the following code:

> > static char* flintstones[] =
> > {
> >     "fred",
> >     "wilma"
> > };

> > static char* jetsons[] =
> > {
> >    "george",
> >    "judy"
> > };

> > and I want to declare another array:

> > //I'm not sure how to do the following here...
> > static char* cartoons[] =
> > {
> >   flintstones,
> >   jetsons
> > }

> The type of 'flintstones' and 'jetsons' is char**, hence you want an array
> of char **:

> static char** cartoons[] =
> {
>    flintstones,
>    jetsons
> };

> #include <stdio.h>

> int main (void)
> {
>    static char *flintstones[] =
>    {
>       "fred",
>       "wilma"
>    };

>    static char *jetsons[] =
>    {
>       "george",
>       "judy"
>    };

>    static char **cartoons[] =
>    {
>       flintstones,
>       jetsons
>    };

>    printf ("%s\n", cartoons[0][0]);
>    printf ("%s\n", cartoons[0][1]);
>    printf ("%s\n", cartoons[1][0]);
>    printf ("%s\n", cartoons[1][1]);

>    return 0;
> }

> C:\CLC\B\BRUMELY>bc
> fred
> wilma
> george
> judy

Great!  Thanks!


Tue, 19 Apr 2005 23:48:31 GMT  
 char array (array of array...) question


Quote:

>> I have the following code:

>> static char* flintstones[] =
>> {
>>     "fred",
>>     "wilma"
>> };

>> static char* jetsons[] =
>> {
>>    "george",
>>    "judy"
>> };

>> and I want to declare another array:

>> //I'm not sure how to do the following here...
>> static char* cartoons[] =
>> {
>>   flintstones,
>>   jetsons
>> }

>The type of 'flintstones' and 'jetsons' is char**,

Techincally, flinstones and jetsons are each arrays of pointers to char,
not pointers to pointers to chars.  In the context they will decay to
char ** when assigned.

John
--
The Linux Emporium - the source for Linux in the UK
See http://www.linuxemporium.co.uk/

We had a woodhenge here once but it rotted.



Fri, 22 Apr 2005 23:48:36 GMT  
 char array (array of array...) question
Groovy hepcat Emmanuel Delahaye was jivin' on 1 Nov 2002 08:34:57 GMT
in comp.lang.c.
Re: char array (array of array...) question's a cool scene! Dig it!

Quote:

>> static char* flintstones[] =
>> {
>>     "fred",
>>     "wilma"
>> };

>> static char* jetsons[] =
>> {
>>    "george",
>>    "judy"
>> };

>> and I want to declare another array:

>> //I'm not sure how to do the following here...
>> static char* cartoons[] =
>> {
>>   flintstones,
>>   jetsons
>> }

>The type of 'flintstones' and 'jetsons' is char**, hence you want an array
>of char **:

  Wrong! The type of flintstones and jetsons is char *[2]. That's an
array of 2 pointers to char. Therefore, what Coleman needs is a char
*(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
char. Eg.:

char *(*cartoons[2])[2] = {&flintstones, &jetsons};

char *cartoon_character = (*cartoons[0])[0];  /* cartoon_character =
                                                 "fred" */

  An array of 2 arrays of 2 pointers to char would be better in some
ways. However, he could not initialise this with flintstones and
jetsons. He'd have to initialise it separately with the same data, or
copy the data from them into it. Eg.:

char *cartoons[2][2] =
{
  {"fred", "wilma"},
  {"george", "judy"}

Quote:
};

        or

char *cartoons[2][2];

memcpy(cartoons[0], flintstones, sizeof flintstones);
memcpy(cartoons[1], jetsons, sizeof jetsons);

The advantage of this is that accessing the character names could be
done the way Coleman wanted. Eg.:

cartoon_character = cartoons[0][0];  /* cartoon_character = "fred" */

But I suggest he use the char *(*[2])[2] method as it avoids
duplication of the data.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?



Sun, 24 Apr 2005 06:24:20 GMT  
 char array (array of array...) question

Quote:
> Groovy hepcat Emmanuel Delahaye was jivin' on 1 Nov 2002 08:34:57 GMT
> in comp.lang.c.
> Re: char array (array of array...) question's a cool scene! Dig it!


>>> static char* flintstones[] =
>>> {
>>>     "fred",
>>>     "wilma"
>>> };

>>> static char* jetsons[] =
>>> {
>>>    "george",
>>>    "judy"
>>> };

>>> and I want to declare another array:

>>> //I'm not sure how to do the following here...
>>> static char* cartoons[] =
>>> {
>>>   flintstones,
>>>   jetsons
>>> }

>>The type of 'flintstones' and 'jetsons' is char**, hence you want an array
>>of char **:

>  Wrong! The type of flintstones and jetsons is char *[2]. That's an
> array of 2 pointers to char. Therefore, what Coleman needs is a char
> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
> char. Eg.:

Ahh, but the type of the _expressions_ (flintstones) and (jetsons) is
char **, so a char **[2] will suffice:

static char **cartoons[] = {
    flintstones,
    jetsons

Quote:
};

and allows accessing through the same types of expressions as the
(*[2])[2] form:

printf("%s\n", cartoons[1][0]);

This has the advantage that it can accomodate differently sized cartoon
families, but the disadvantage that you can't use sizeof on the members
of cartoons to discover the sizes of the original array - so to take
advantage of the flexibility, you'd probably want to use a null sentinel
in the name arrays:

static char* flintstones[] =
{
    "fred",
    "wilma",
    NULL

Quote:
};

This lets you do:

for (i = 0; i < ((sizeof cartoons)/(sizeof cartoons[0])); i++)
    for (j = 0; cartoons[i][j] != NULL; j++)
        puts(cartoons[i][j]);

        - Kevin.



Sun, 24 Apr 2005 11:35:32 GMT  
 char array (array of array...) question

Quote:


>>>Therefore, what Coleman needs is a char
>>> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
>>> char. Eg.:

> Wrong.  When assigned the types will decay appropriately.  

Conceptually the decay occurs before the assignment.  In fact, the
assignment is irrelevant.  Conversion to pointer-to-first-element is
the normal case.  The expression on the second line of the following
code has type `char **'.

  char *foo[] = {"one", "two"};
  foo;

Jeremy.



Sun, 24 Apr 2005 21:27:21 GMT  
 char array (array of array...) question


Quote:

>> Groovy hepcat Emmanuel Delahaye was jivin' on 1 Nov 2002 08:34:57 GMT
>> in comp.lang.c.
>> Re: char array (array of array...) question's a cool scene! Dig it!


>>>> static char* flintstones[] =
>>>> {
>>>>     "fred",
>>>>     "wilma"
>>>> };

>>>> static char* jetsons[] =
>>>> {
>>>>    "george",
>>>>    "judy"
>>>> };

>>>> and I want to declare another array:

>>>> //I'm not sure how to do the following here...
>>>> static char* cartoons[] =
>>>> {
>>>>   flintstones,
>>>>   jetsons
>>>> }

>>>The type of 'flintstones' and 'jetsons' is char**, hence you want an array
>>>of char **:

>>  Wrong! The type of flintstones and jetsons is char *[2]. That's an
>> array of 2 pointers to char.

Correct.

Quote:
>>Therefore, what Coleman needs is a char
>> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
>> char. Eg.:

Wrong.  When assigned the types will decay appropriately.  Just as you
can assign something of type "array of char" to a variable of type
"pointer to char", so you can assign something of type "array of pointer
to char" to a variable of type "pointer to pointer to char".

In general, you can assign something of type "array of X" to a variable
of type "pointer to X".

Quote:
>Ahh, but the type of the _expressions_ (flintstones) and (jetsons) is
>char **,

Still not true.  Try the following program.

#include <stdio.h>

static char *flintstones[] =
{
    "fred", "wilma"

Quote:
};

static char **banana;

int main (void)
{
    printf ("Sizeof flintsones = %d.\n", (int) sizeof (flintstones));
    printf ("Sizeof banana     = %d.\n", (int) sizeof (banana));
    return 0;

Quote:
}

John
--
The Linux Emporium - the source for Linux in the UK
See http://www.linuxemporium.co.uk/

We had a woodhenge here once but it rotted.



Sun, 24 Apr 2005 21:18:47 GMT  
 char array (array of array...) question

Quote:




>>> Groovy hepcat Emmanuel Delahaye was jivin' on 1 Nov 2002 08:34:57 GMT
>>> in comp.lang.c.
>>> Re: char array (array of array...) question's a cool scene! Dig it!


>>>>> static char* flintstones[] =
>>>>> {
>>>>>     "fred",
>>>>>     "wilma"
>>>>> };

>>>>> static char* jetsons[] =
>>>>> {
>>>>>    "george",
>>>>>    "judy"
>>>>> };

>>>>> and I want to declare another array:

>>>>> //I'm not sure how to do the following here...
>>>>> static char* cartoons[] =
>>>>> {
>>>>>   flintstones,
>>>>>   jetsons
>>>>> }

>>>>The type of 'flintstones' and 'jetsons' is char**, hence you want an array
>>>>of char **:

>>>  Wrong! The type of flintstones and jetsons is char *[2]. That's an
>>> array of 2 pointers to char.

> Correct.

>>>Therefore, what Coleman needs is a char
>>> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
>>> char. Eg.:

> Wrong.  When assigned the types will decay appropriately.  Just as you
> can assign something of type "array of char" to a variable of type
> "pointer to char", so you can assign something of type "array of pointer
> to char" to a variable of type "pointer to pointer to char".

> In general, you can assign something of type "array of X" to a variable
> of type "pointer to X".

>>Ahh, but the type of the _expressions_ (flintstones) and (jetsons) is
>>char **,

> Still not true.  Try the following program.

> #include <stdio.h>

> static char *flintstones[] =
> {
>    "fred", "wilma"
> };

> static char **banana;

> int main (void)
> {
>    printf ("Sizeof flintsones = %d.\n", (int) sizeof (flintstones));

(flintstones) there is a subexpression, not a complete expression.

        - Kevin.



Sun, 24 Apr 2005 21:46:45 GMT  
 char array (array of array...) question


Quote:



[snip]
>>>Ahh, but the type of the _expressions_ (flintstones) and (jetsons) is
>>>char **,

>> Still not true.  Try the following program.

>> #include <stdio.h>

>> static char *flintstones[] =
>> {
>>    "fred", "wilma"
>> };

>> static char **banana;

>> int main (void)
>> {
>>    printf ("Sizeof flintsones = %d.\n", (int) sizeof (flintstones));

>(flintstones) there is a subexpression, not a complete expression.

So what you're claiming is that when the expression "(flintstones)" is on
its own it has a type of char ** but when it's embedded in a larger
expression and thus becomes a sub-expression it remembers an earlier
type?  It seems a little unlikely that an expression could have this
quality.

John
--
The Linux Emporium - the source for Linux in the UK
See http://www.linuxemporium.co.uk/

We had a woodhenge here once but it rotted.



Sun, 24 Apr 2005 22:57:42 GMT  
 char array (array of array...) question


Quote:


>>>>Therefore, what Coleman needs is a char
>>>> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
>>>> char. Eg.:

>> Wrong.  When assigned the types will decay appropriately.  

>Conceptually the decay occurs before the assignment.

I wouldn't argue with that.  However my point is that the type doesn't
always decay, and it thus isn't correct to say that the type of flintstones
is char **.

John
--
The Linux Emporium - the source for Linux in the UK
See http://www.linuxemporium.co.uk/

We had a woodhenge here once but it rotted.



Sun, 24 Apr 2005 22:59:35 GMT  
 char array (array of array...) question

Quote:






> [snip]
>>>>Ahh, but the type of the _expressions_ (flintstones) and (jetsons) is
>>>>char **,

>>> Still not true.  Try the following program.

>>> #include <stdio.h>

>>> static char *flintstones[] =
>>> {
>>>    "fred", "wilma"
>>> };

>>> static char **banana;

>>> int main (void)
>>> {
>>>    printf ("Sizeof flintsones = %d.\n", (int) sizeof (flintstones));

>>(flintstones) there is a subexpression, not a complete expression.

> So what you're claiming is that when the expression "(flintstones)" is on
> its own it has a type of char ** but when it's embedded in a larger
> expression and thus becomes a sub-expression it remembers an earlier
> type?  It seems a little unlikely that an expression could have this
> quality.

And yet, it does have this quality.

6.3.2.1 #3
Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type "array of type" is converted to an expression
with type "pointer to type" that points to the initial element of the
array object and is not an lvalue. If the array object has register
storage class, the behavior is undefined.

When it's a complete expression, (flintstones) has pointer type.  This
is particularly applicable here, because such complete expressions can
be used in the initialiser of another array.

static char **cartoons[] = {
     flintstones,
     jetsons

Quote:
};

The first expression in that initialiser list, which consists only of
"flintstones", has type char **.

        - Kevin.



Mon, 25 Apr 2005 09:05:23 GMT  
 char array (array of array...) question
Groovy hepcat John Winters was jivin' on 6 Nov 2002 13:18:47 -0000 in
comp.lang.c.
Re: char array (array of array...) question's a cool scene! Dig it!

Quote:



>>> Groovy hepcat Emmanuel Delahaye was jivin' on 1 Nov 2002 08:34:57 GMT
>>> in comp.lang.c.
>>> Re: char array (array of array...) question's a cool scene! Dig it!


>>>>> static char* flintstones[] =
>>>>> {
>>>>>     "fred",
>>>>>     "wilma"
>>>>> };

>>>>> static char* jetsons[] =
>>>>> {
>>>>>    "george",
>>>>>    "judy"
>>>>> };

>>>>> and I want to declare another array:

>>>>> //I'm not sure how to do the following here...
>>>>> static char* cartoons[] =
>>>>> {
>>>>>   flintstones,
>>>>>   jetsons
>>>>> }

>>>>The type of 'flintstones' and 'jetsons' is char**, hence you want an array
>>>>of char **:

>>>  Wrong! The type of flintstones and jetsons is char *[2]. That's an
>>> array of 2 pointers to char.

>Correct.

>>>Therefore, what Coleman needs is a char
>>> *(*[2])[2]. That's an array of 2 pointers to arrays of 2 pointers to
>>> char. Eg.:

>Wrong.  When assigned the types will decay appropriately.  Just as you

  After the incorrect assertion that flintstones and jetsons were
pointers, I went into debunk mode. I wasn't paying enough attention to
the code, and assumed that it was based on a false premise, so needed
to be debunked. I offer my appologies to the OP and to Emmanuel! The
code he posted was indeed correct.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?



Thu, 28 Apr 2005 09:20:50 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Differences between char array[SIZE] and char *array

2. A char pointer (char *) vs. char array question

3. Managed array of managed arrays of char

4. Counting array char with another array

5. char array to float array

6. Newbie Array Problem - how to strncpy from an array of char

7. Allocating a array of char arrays???

8. Question about adding an array to existing array.

9. Question: extern *array and array[]

10. Converting char array of literals chars to escape chars

11. Question about char arrays

12. question about array of pointers to chars

 

 
Powered by phpBB® Forum Software