initialize an array of elements that contain another array

Discussion in 'C Programming' started by wenmang@yahoo.com, Jul 31, 2006.

  1. Guest

    hi,
    I have following:
    struct array1
    {
    int id;
    char *name;
    };

    struct array2
    {
    int id;
    char * name;
    array1 *myArray;
    };

    Now, I try to create an array of array2 by initialization:
    array2 collection[] =
    {
    10, "first",
    {1, "first.first"},
    20, "second",
    {2, "second.second"}
    };

    compiler generates following similar error:
    error: a value of type "1" cannot be used to initialize an entity of
    type "array1 *"

    How am I going to initialize something like above. Thx
    , Jul 31, 2006
    #1
    1. Advertising

  2. Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1


    wrote:
    > hi,
    > I have following:
    > struct array1
    > {
    > int id;
    > char *name;
    > };
    >
    > struct array2
    > {
    > int id;
    > char * name;
    > array1 *myArray;
    > };
    >
    > Now, I try to create an array of array2 by initialization:
    > array2 collection[] =
    > {
    > 10, "first",
    > {1, "first.first"},
    > 20, "second",
    > {2, "second.second"}
    > };
    >
    > compiler generates following similar error:
    > error: a value of type "1" cannot be used to initialize an entity of
    > type "array1 *"
    >
    > How am I going to initialize something like above. Thx


    You are going to explicitly malloc() space for your array1 entries, and
    populate the myArray pointers in array2 with the malloc()ed values.

    You are going to do this in program logic.

    You are *not* going to do this with an initializer.

    HTH
    - --
    Lew Pitcher


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

    iD8DBQFEzltYagVFX4UWr64RAh+EAJ0XihKh3M+dhmO3sAad8tsmGutOFACgq/UR
    bDbPZO+MoKcIVOGHvLxgTKA=
    =bwDm
    -----END PGP SIGNATURE-----
    Lew Pitcher, Jul 31, 2006
    #2
    1. Advertising


  3. > array1 *myArray;



    Change to:

    array1 myArray;

    --

    Frederick Gotham
    Frederick Gotham, Jul 31, 2006
    #3
  4. Guest

    Lew Pitcher wrote:

    > You are going to explicitly malloc() space for your array1 entries, and
    > populate the myArray pointers in array2 with the malloc()ed values.
    >
    > You are going to do this in program logic.
    >
    > You are *not* going to do this with an initializer.
    >
    > HTH
    > - --
    > Lew Pitcher
    >
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12
    >
    > iD8DBQFEzltYagVFX4UWr64RAh+EAJ0XihKh3M+dhmO3sAad8tsmGutOFACgq/UR
    > bDbPZO+MoKcIVOGHvLxgTKA=
    > =bwDm
    > -----END PGP SIGNATURE-----



    actually, I want an array1 inside array2 without specifying the size of
    array1. The size of array1 is determined through initialization list,
    can I do that?
    struct array2
    {
    int id;
    char *name;
    array1 myArray[]; - will not compile here
    };
    , Jul 31, 2006
    #4
  5. Guest

    struct type1
    {
    int id;
    char *name;
    };


    struct type2
    {
    int id;
    char * name;
    type1 *myType1Collection; //should be arrary of type1 here: type1
    myType1Collcetion[]?

    };

    The actual array size for type1 is a variable and can only determined
    through initialization list after reading from external input through a
    code generation routine. How can I achieve that?
    , Jul 31, 2006
    #5
  6. Guest

    struct type1
    {
    int id;
    char *name;
    };


    struct type2
    {
    int id;
    char * name;
    type1 *myType1Collection; //should be arrary of type1 here: type1
    myType1Collcetion[]?

    };

    The actual array size for type1 is a variable and can only determined
    through initialization list after reading from external input through a
    code generation routine. How can I achieve that?
    , Jul 31, 2006
    #6
  7. > wrote:
    > hi,
    > I have following:
    > struct array1
    > {
    > int id;
    > char *name;
    > };


    This is ok.

    > struct array2
    > {
    > int id;
    > char * name;
    > array1 *myArray;
    > };


    The last member makes me worry. It is not an array but a pointer. Moreover,
    you did not define the array1 type. If you are using a C++ compiler, this
    is not a problem, but, well, in C, you'd better write
    struct array1 *myArray;
    But as already told, this is not an array. An array looks like
    struct array1 myArray[5];

    but then you fix the number of elements. You could also write
    struct array1 myArray[];
    but then you would like to define an array out ouf struct array2 which.
    Should not all the members of this array be of the same type?
    (Different sizes of myArrays make them incompatible.)

    Szabolcs Borsanyi
    Szabolcs Borsanyi, Jul 31, 2006
    #7
  8. writes:
    > I have following:
    > struct array1
    > {
    > int id;
    > char *name;
    > };
    >
    > struct array2
    > {
    > int id;
    > char * name;
    > array1 *myArray;
    > };
    >
    > Now, I try to create an array of array2 by initialization:
    > array2 collection[] =
    > {
    > 10, "first",
    > {1, "first.first"},
    > 20, "second",
    > {2, "second.second"}
    > };
    >


    The first thing you should do is stop compiling your code with a C++
    compiler; either that, or ask in comp.lang.c++.

    You haven't declared a type "array1", so the declaration of the third
    element of your "struct array2" is illegal. You've declared a type
    called "struct array1".

    The following would be legal:

    struct array1
    {
    int id;
    char *name;
    };

    struct array2
    {
    int id;
    char *name;
    struct array1 *myArray;
    };

    struct array2 collection[] = ...

    [...]

    > How am I going to initialize something like above. Thx


    C allows you to omit some braces in an initializer, but it's not
    generally a good idea to take advantage of this. Enclosing the
    initializers for each object and subobject in braces can make the code
    easier to understand; it can also make it easier for the compiler to
    catch some errors.

    Assuming you want two elements in your array, you can declare it like
    this:

    struct array2 collection[] =
    { { 10, "first", ? },
    { 20, "second", ? } };

    The question marks, of course, aren't legal; they need to be replaced
    by something that initializes the myArray member of the struct array2
    object.

    What you want to do is initialize the myArray member (a pointer to
    struct array1) so it points to an object of type struct array1. To do
    this, you would need to allocate an object of type struct array1. You
    can't really do this in an initializer.

    Here's one possible approach:

    struct array1
    {
    int id;
    char *name;
    };

    struct array2
    {
    int id;
    char *name;
    struct array1 *myArray;
    };

    struct array1 a1_0 = { 1, "first.first" };
    struct array1 a1_1 = { 2, "second.second" };

    struct array2 collection[] =
    { { 10, "first", &a1_0 },
    { 20, "second", &a1_1 } };

    Incidentally, "array1" and "array2" aren't very good names for
    structures. I hope you're using something clearer in your real code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 31, 2006
    #8
  9. On 31 Jul 2006 12:28:16 -0700, wrote:

    >hi,
    >I have following:
    >struct array1
    >{
    > int id;
    > char *name;
    >};
    >
    >struct array2
    >{
    > int id;
    > char * name;
    > array1 *myArray;
    >};


    array2 has three members.
    >
    >Now, I try to create an array of array2 by initialization:
    >array2 collection[] =
    >{
    > 10, "first",


    By omitting the braces on this line, you are telling the compiler that
    this is not the complete list of initialization values for the first
    element of the array. Since there are two values in this list, they
    are assigned to id and name. The next value will be assigned to
    myArray (which has type pointer to struct).


    > {1, "first.first"},


    This is not a suitable value for myArray. If myArray were a struct
    instead of a pointer to struct, it would probably work.

    > 20, "second",
    > {2, "second.second"}
    >};
    >
    >compiler generates following similar error:
    > error: a value of type "1" cannot be used to initialize an entity of
    >type "array1 *"
    >
    >How am I going to initialize something like above. Thx


    If you really want myArray to be a pointer, then you will need to
    define a number of objects of type struct array1 (probably initialized
    with the braced values above) and replace the braced initialization
    values above with the address of the appropriate object (e.g.,
    &array1_1).


    Remove del for email
    Barry Schwarz, Aug 1, 2006
    #9
  10. Simon Biber Guest

    wrote:
    > hi,
    > I have following:
    > struct array1
    > {
    > int id;
    > char *name;
    > };
    >
    > struct array2
    > {
    > int id;
    > char * name;
    > array1 *myArray;
    > };

    ....
    > How am I going to initialize something like above. Thx


    Hi Wenmang. (IIRC that means 'illiterate' in Chinese?)

    Nobody here mentioned that you can use a feature from C99 to create an
    automatic struct and take its address as part of the initialiser.

    #include <stdio.h>

    struct foo
    {
    int id;
    char *name;
    };

    struct bar
    {
    int id;
    char *name;
    struct foo *pfoo;
    };

    int main(void)
    {
    struct bar myBar = {
    10,
    "hello",
    & (struct foo) {20, "world"},
    };
    printf("{%d, %s, {%d, %s}}\n",
    myBar.id, myBar.name,
    myBar.pfoo->id, myBar.pfoo->name);
    return 0;
    }

    Note that the unnamed struct object that was created has function scope.
    If you do the initialisation inside a function and then return from the
    function, the pointer will become invalid.

    --
    Simon.
    Simon Biber, Aug 2, 2006
    #10
  11. Guest


    > Hi Wenmang. (IIRC that means 'illiterate' in Chinese?)


    as matter of fact, yes. hehihei. Surprised you know some chinese
    , Aug 2, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hyena
    Replies:
    10
    Views:
    15,381
  2. Mars

    Initialize elements in an array

    Mars, Jan 27, 2005, in forum: C Programming
    Replies:
    5
    Views:
    381
    infobahn
    Jan 28, 2005
  3. syang8
    Replies:
    4
    Views:
    721
    James Kanze
    Jun 21, 2008
  4. Roger Pack
    Replies:
    3
    Views:
    157
    Roger Pack
    Sep 28, 2010
  5. Jason Carlton
    Replies:
    11
    Views:
    247
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page