Help with struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Gerrard

    #1

    Help with struct

    Hi, this is my second week in C and this group.

    This code compiles cleanly.

    //a bit of this is copied from
    //page 133 of K&R, 2nd edition
    struct key
    {
    char *word;
    int count;
    } keytab[]=
    {
    {"auto",0},
    {"break",0}
    };


    My code, which represents what I want to do, does not. I am sure it is
    a syntax problem but could someone guide me on the type of syntax I
    should use. My code for the structure is below.

    struct teststr
    {
    char *word;
    int[][] array;
    } test[]=
    {
    {"1",{{1,2,3},{ 4,5,6},{7,8,9}} ,
    {"2",{{1,2,3},{ 4,5,6}}
    };

    I don't want you to think I am lazy and not coding this myself. So here
    is what I think. I can't have variable length arrays in the struct. I
    think my approach should create a struct so that "array" is a pointer
    to each of the variable length arrays so that each element in array
    "test" is the same size. I would appreciate someone confirming this or
    putting me on the right track, (and if it's difficult, a bit of
    pseudocode so I get there).

    Thank you again
    John

  • Richard Heathfield

    #2
    Re: Help with struct

    John Gerrard said:

    <snip>
    >
    struct teststr
    {
    char *word;
    int[][] array;
    K&R, 2nd edition, page 112, sentence beginning "More generally", about four
    or five lines from the bottom.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • John Gerrard

      #3
      Re: Help with struct

      "More generally, only the first dimension(subsc ript) of an array is
      free; all the others have to be specified. Section 5.12 has a further
      discussion..."

      Thanks
      John


      Richard Heathfield wrote:
      John Gerrard said:
      >
      <snip>

      struct teststr
      {
      char *word;
      int[][] array;
      >
      K&R, 2nd edition, page 112, sentence beginning "More generally", about four
      or five lines from the bottom.
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      Working...