Another c struct syntax question

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

    #1

    Another c struct syntax question

    I'm sorry for the second post in as many hours, and both about structs.

    The code below compiles. The variables test1 and test2 are structures
    of datatype teststruct1 and teststruct2. The size of testintArray is
    explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
    define a generic structure so that testintArray points to an array of
    ints. The generic structure I have written is teststruct3.

    What I can't seem to figure out is the syntax how to initialise test3
    with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
    I have had to do it a long way with malloc etc in function main. Can I
    initialise the test3 instance of the struct teststring3 using a syntax
    like{1,{1,2,3}} ?

    I would appreciate help with the syntax. Thank you.
    Yusuf


    #include <stdlib.h>

    struct teststruct1
    {
    int testint;
    int testintArray[2];
    } test1[3] =
    {
    {1,{1,2}},
    {2,{1,9}},
    {3,{2,9}}
    };

    struct teststruct2
    {
    int testint;
    int testintArray[3];
    } test2[3] =
    {
    {1,{1,2,3}},
    {2,{1,4,5}},
    {3,{2,3,1}}
    };

    //I want to initialise test3 like I have
    //done with test2 and test1 above using the
    //easy to read {1,{1,2,3}} syntax or something
    //like it.
    struct teststruct3
    {
    int testint;
    int *testintArray;
    } test3[3];

    int main(void)
    {
    test3[0].testint=1;
    int *testArray=mall oc(5*sizeof(int ));
    testArray[0]=1;
    testArray[1]=2;
    testArray[2]=4;
    testArray[3]=7;
    testArray[4]=7;
    test3[0].testintArray=t estArray;
    return 0;
    }

  • Jack Klein

    #2
    Re: Another c struct syntax question

    On 23 Sep 2006 09:14:34 -0700, "Yusuf" <askhfaskf123@h otmail.com>
    wrote in comp.lang.c:
    I'm sorry for the second post in as many hours, and both about structs.
    >
    The code below compiles. The variables test1 and test2 are structures
    of datatype teststruct1 and teststruct2. The size of testintArray is
    explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
    define a generic structure so that testintArray points to an array of
    ints. The generic structure I have written is teststruct3.
    >
    What I can't seem to figure out is the syntax how to initialise test3
    with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
    I have had to do it a long way with malloc etc in function main. Can I
    initialise the test3 instance of the struct teststring3 using a syntax
    like{1,{1,2,3}} ?
    >
    I would appreciate help with the syntax. Thank you.
    Yusuf
    >
    >
    #include <stdlib.h>
    >
    struct teststruct1
    {
    int testint;
    int testintArray[2];
    } test1[3] =
    {
    {1,{1,2}},
    {2,{1,9}},
    {3,{2,9}}
    };
    >
    struct teststruct2
    {
    int testint;
    int testintArray[3];
    } test2[3] =
    {
    {1,{1,2,3}},
    {2,{1,4,5}},
    {3,{2,3,1}}
    };
    >
    //I want to initialise test3 like I have
    //done with test2 and test1 above using the
    //easy to read {1,{1,2,3}} syntax or something
    //like it.
    You can't initialize pointers this way, especially not at constant
    scope, with the exception of pointers to char which can be initialized
    with a string literal. What you can do is this:

    int array1 [] = { 1, 3, 4, 7, 7 };
    int array2 [] = { 3, 5, 7, 9, 12 };
    int array3 [] = { 2, 4, 6, 8, 10, 12, 14 };
    struct teststruct3
    {
    int testint;
    int *testintArray;
    };
    struct teststruct3 test3 [3] =
    {
    { sizeof array1 / sizeof *array1, array1 };
    { sizeof array2 / sizeof *array2, array2 };
    { sizeof array3 / sizeof *array3, array3 };
    };

    Note that in my experience most experienced C programmers frown on
    defining a type and an object or array of that type in the same source
    construct. Let alone initializing.

    The way I have done it adds some extra white space to the source,
    which is completely free, and an extra "struct teststruct3", cheap,
    and an extra ';', very very cheap.
    int main(void)
    {
    test3[0].testint=1;
    int *testArray=mall oc(5*sizeof(int ));
    testArray[0]=1;
    testArray[1]=2;
    testArray[2]=4;
    testArray[3]=7;
    testArray[4]=7;
    test3[0].testintArray=t estArray;
    return 0;
    }
    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Harald van Dijk

      #3
      Re: Another c struct syntax question

      Yusuf wrote:
      I'm sorry for the second post in as many hours, and both about structs.
      >
      The code below compiles. The variables test1 and test2 are structures
      of datatype teststruct1 and teststruct2. The size of testintArray is
      explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
      define a generic structure so that testintArray points to an array of
      ints. The generic structure I have written is teststruct3.
      >
      What I can't seem to figure out is the syntax how to initialise test3
      with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
      I have had to do it a long way with malloc etc in function main. Can I
      initialise the test3 instance of the struct teststring3 using a syntax
      like{1,{1,2,3}} ?
      In C89, you cannot. In C99, you can. It's very well possible that
      whatever compiler you are using does not support C99, though.

      [...]
      struct teststruct3
      {
      int testint;
      int *testintArray;
      } test3[3]
      = {
      { 1, (int []) { 1, 2, 3 } },
      { 2, (int []) { 2, 4, 6 } },
      { 3, (int []) { 3, 6, 9 } },
      };

      Comment

      • Hubble

        #4
        Re: Another c struct syntax question

        Yusuf schrieb:
        //I want to initialise test3 ...
        struct teststruct3
        {
        int testint;
        int *testintArray;
        } test3[3];
        >
        int main(void)
        {
        test3[0].testint=1;
        int *testArray=mall oc(5*sizeof(int ));
        testArray[0]=1;
        testArray[1]=2;
        testArray[2]=4;
        testArray[3]=7;
        testArray[4]=7;
        test3[0].testintArray=t estArray;
        return 0;
        }
        You can use an additional "helper" arrays:

        int testintArray0[5]={ 1, 2, 4, 7, 7 };
        int testintArray1[5]={ 1, 1, 1, 1, 1 };
        int testintArray2[5]={ 1, 2, 3, 4, 5 };
        struct teststruct3
        {
        int testint;
        int *testintArray;
        } test3[3]=
        { { 1, testintArray0 },
        { 2, testintArray1 },
        { 3, testintArray2 }
        };

        Hubble

        Comment

        • kondal

          #5
          Re: Another c struct syntax question


          Yusuf wrote:
          I'm sorry for the second post in as many hours, and both about structs.
          >
          The code below compiles. The variables test1 and test2 are structures
          of datatype teststruct1 and teststruct2. The size of testintArray is
          explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
          define a generic structure so that testintArray points to an array of
          ints. The generic structure I have written is teststruct3.
          >
          What I can't seem to figure out is the syntax how to initialise test3
          with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
          I have had to do it a long way with malloc etc in function main. Can I
          initialise the test3 instance of the struct teststring3 using a syntax
          like{1,{1,2,3}} ?
          >
          I would appreciate help with the syntax. Thank you.
          Yusuf
          >
          >
          #include <stdlib.h>
          >
          struct teststruct1
          {
          int testint;
          int testintArray[2];
          } test1[3] =
          {
          {1,{1,2}},
          {2,{1,9}},
          {3,{2,9}}
          };
          >
          struct teststruct2
          {
          int testint;
          int testintArray[3];
          } test2[3] =
          {
          {1,{1,2,3}},
          {2,{1,4,5}},
          {3,{2,3,1}}
          };
          >
          //I want to initialise test3 like I have
          //done with test2 and test1 above using the
          //easy to read {1,{1,2,3}} syntax or something
          //like it.
          struct teststruct3
          {
          int testint;
          int *testintArray;
          } test3[3];
          >
          int main(void)
          {
          test3[0].testint=1;
          int *testArray=mall oc(5*sizeof(int ));
          testArray[0]=1;
          testArray[1]=2;
          testArray[2]=4;
          testArray[3]=7;
          testArray[4]=7;
          test3[0].testintArray=t estArray;
          return 0;
          }
          If you are using another array to initialize your structures, why don't
          you use memcpy.

          Create a integer pointer to the structure and memcpy the entire
          structure. If not you can use it to initialize individually.

          -kondal

          Comment

          • Yusuf

            #6
            Re: Another c struct syntax question

            Thanks very much for taking the time to post back in this thread
            everyone.
            Regards
            Yusuf

            Comment

            • Yusuf

              #7
              Re: Another c struct syntax question

              Thanks for this Harald. I am using a c99 flag in gcc so the syntax is
              fine.

              What I want to do using this syntax is initialise teststruct4. In my
              code teststruct4a is legal and the compiler does not complain. But the
              compiler does complain about teststruct4b. The error is:

              t2.c:31: error: 'teststruct3' undeclared here (not in a function)
              t2.c:32: error: expected '}' before '{' token
              t2.c:37: warning: excess elements in struct initializer
              t2.c:37: warning: (near initialization for 'test4b[0]')

              I want to initialise teststruct4b inline as I have done, through your
              help, with teststruct3. I haven't got the syntax for teststruct4b
              correct. My code is below.
              Thank you to everyone in this group for giving up their time helping me
              learn c.
              Yusuf

              struct teststruct3
              {
              int testint;
              int *testintArray;
              };

              struct teststruct3 test3[3] =
              {
              {1, (int []) { 1, 2, 3 } },
              {2, (int []) { 3, 4, 5 } },
              {3, (int []) { 6, 7, 8 } }
              };


              struct teststruct4a
              {
              char *testchar;
              struct teststruct3 *teststruct;
              } test4a[1] =
              {
              {"testchar",tes t3}
              };


              struct teststruct4b
              {
              char *testchar;
              struct teststruct3 *teststruct;
              } test4b[1] =
              {
              {"testchar",(te ststruct3)
              {
              {1, (int []) { 1, 2, 3 } },
              {2, (int []) { 3, 4, 5 } },
              {3, (int []) { 6, 7, 8 } }
              }
              }
              };


              int main(void)
              {
              return 0;
              }

              Comment

              • Harald van Dijk

                #8
                Re: Another c struct syntax question

                Yusuf wrote:
                struct teststruct4b
                {
                char *testchar;
                struct teststruct3 *teststruct;
                } test4b[1] =
                {
                {"testchar",(te ststruct3)
                {
                {1, (int []) { 1, 2, 3 } },
                {2, (int []) { 3, 4, 5 } },
                {3, (int []) { 6, 7, 8 } }
                }
                }
                };
                You haven't defined any typedef named teststruct3. You have defined a
                struct type named teststruct3, but you can only refer to that as
                "struct teststruct3". And you want more than one instance of that
                struct, but you only ask for one.

                (struct teststruct3 []) { ... } should work.

                Comment

                • Yusuf

                  #9
                  Re: Another c struct syntax question

                  Thank you Harald for the very fast reply. It does work now!

                  As you might have guessed, I am not doing this for a specific program,
                  I am doing this to learn c. Just to follow-up the alternate typedef
                  approach you mentioned and how I hadn't defined any typedef named
                  teststruct3, in my sample code below I have modified it so I now have
                  so I can look at this too. But the compiler is now complaining because
                  my code is wrong again. Again I've tried lots of syntax but I just
                  don't have the experience yet. Could you advise on what the problem is
                  here with teststruct4b now (note teststruct3 is now typedef). The error
                  is:

                  t2.c:26: warning: initialization from incompatible pointer type


                  Code below.
                  Thank you again
                  Yusaf


                  typedef struct
                  {
                  int testint;
                  int *testintArray;
                  } teststruct3;

                  struct teststruct4a
                  {
                  char *testchar;
                  struct teststruct3 *teststruct;
                  };

                  struct teststruct4b
                  {
                  char *testchar;
                  struct teststruct3 *teststruct;
                  } test4b[1] =
                  {
                  {"testchar",(te ststruct3[])
                  {
                  {1, (int []) { 1, 2, 3 } },
                  {2, (int []) { 3, 4, 5 } },
                  {3, (int []) { 6, 7, 8 } }
                  }
                  }
                  };

                  int main(void)
                  {
                  return 0;
                  }

                  Comment

                  • Harald van Dijk

                    #10
                    Re: Another c struct syntax question

                    Yusuf wrote:
                    Thank you Harald for the very fast reply. It does work now!
                    >
                    As you might have guessed, I am not doing this for a specific program,
                    I am doing this to learn c. Just to follow-up the alternate typedef
                    approach you mentioned and how I hadn't defined any typedef named
                    teststruct3, in my sample code below I have modified it so I now have
                    so I can look at this too. But the compiler is now complaining because
                    my code is wrong again. Again I've tried lots of syntax but I just
                    don't have the experience yet. Could you advise on what the problem is
                    here with teststruct4b now (note teststruct3 is now typedef). The error
                    is:
                    The initialisation is correct, but this time, the declaration is valid
                    but different from what you intended.
                    t2.c:26: warning: initialization from incompatible pointer type
                    [...]
                    typedef struct
                    {
                    int testint;
                    int *testintArray;
                    } teststruct3;
                    >
                    struct teststruct4a
                    {
                    char *testchar;
                    struct teststruct3 *teststruct;
                    };
                    You're declaring a new type "struct teststruct3", which is different
                    from "teststruct 3". Try "teststruct 3 *teststruct" instead. (And do the
                    same for teststruct4b.)

                    Comment

                    • Yusuf

                      #11
                      Re: Another c struct syntax question

                      Thank you again Harald. This works too! I have really got to sit down
                      and study this code now and convince myself I really do understand it.
                      Regards
                      Yusuf

                      Comment

                      • Keith Thompson

                        #12
                        Re: Another c struct syntax question

                        "kondal" <kondal04@gmail .comwrites:
                        [...]
                        If you are using another array to initialize your structures, why don't
                        you use memcpy.
                        >
                        Create a integer pointer to the structure and memcpy the entire
                        structure. If not you can use it to initialize individually.
                        What do you mean by "integer pointer"? Do you mean just "pointer"?

                        It's not necessary to use memcpy() to copy structures; you can just
                        use an assignment.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                        Comment

                        • Keith Thompson

                          #13
                          Re: Another c struct syntax question

                          "Yusuf" <askhfaskf123@h otmail.comwrite s:
                          [...]
                          t2.c:26: warning: initialization from incompatible pointer type
                          >
                          >
                          Code below.
                          Thank you again
                          Yusaf
                          >
                          >
                          typedef struct
                          {
                          int testint;
                          int *testintArray;
                          } teststruct3;
                          >
                          struct teststruct4a
                          {
                          char *testchar;
                          struct teststruct3 *teststruct;
                          };
                          >
                          struct teststruct4b
                          {
                          char *testchar;
                          struct teststruct3 *teststruct;
                          } test4b[1] =
                          {
                          {"testchar",(te ststruct3[])
                          {
                          {1, (int []) { 1, 2, 3 } },
                          {2, (int []) { 3, 4, 5 } },
                          {3, (int []) { 6, 7, 8 } }
                          }
                          }
                          };
                          >
                          int main(void)
                          {
                          return 0;
                          }
                          For the sake of consistency, don't use typedefs. You originally
                          declared your "teststruct 3" type as "struct teststruct3 { ... };";
                          there was no reason to change that to a typedef. The problem was
                          that, later on, you referred to the type as "teststruct 3". You just
                          need to refer to it as "struct teststruct3".

                          A typedef for a struct type merely declares another name for something
                          that already has one. Some people like being able to refer to the
                          type with a single identifier, but it's not at all necessary.

                          (If you're going to use typedefs, you should be consistent; having
                          "teststruct 3" as a typedef name and "teststruct 4b" as a struct tag is
                          just confusing.)

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                          Comment

                          Working...