How to initialize and array in a structure?

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

    #1

    How to initialize and array in a structure?

    How do I set the value of a[0] = 3 in the following lines of code?

    #include <stdio.h>

    struct letter{
    char a[0];
    };

    struct add {
    struct letter addit;
    };

    int main(void) {
    struct add test;
    return 0;
    }

    when I try something like test.add=3 or test.addit.a=3, the compiler
    gives me the warning
    'incompatible types in assignment'.

    Thanks in advance
    Chad

  • Neil Kurzman

    #2
    Re: How to initialize and array in a structure?



    Chad wrote:
    [color=blue]
    > How do I set the value of a[0] = 3 in the following lines of code?
    >
    > #include <stdio.h>
    >
    > struct letter{
    > char a[0];
    > };
    >
    > struct add {
    > struct letter addit;
    > };
    >
    > int main(void) {
    > struct add test;
    > return 0;
    > }
    >
    > when I try something like test.add=3 or test.addit.a=3, the compiler
    > gives me the warning
    > 'incompatible types in assignment'.
    >
    > Thanks in advance
    > Chad[/color]

    char a[0] is a zero length array you can not set it to any thing.
    assuming
    char a[1];
    try
    test.add[0]=3;


    Comment

    • Artie Gold

      #3
      Re: How to initialize and array in a structure?

      Chad wrote:[color=blue]
      > How do I set the value of a[0] = 3 in the following lines of code?
      >
      > #include <stdio.h>
      >
      > struct letter{
      > char a[0];[/color]
      An array with a length of 0 is not permitted in C. ITYM:
      char a[1];
      (Which doesn't make a whole lot of sense either.)[color=blue]
      > };
      >
      > struct add {
      > struct letter addit;
      > };
      >
      > int main(void) {
      > struct add test;[/color]
      /* assignment */
      test.addit.a[0] = 3;[color=blue]
      > return 0;
      > }
      >
      > when I try something like test.add=3 or test.addit.a=3, the compiler
      > gives me the warning
      > 'incompatible types in assignment'.
      >[/color]
      Of course, you need to realize that assignment and initialization are
      two very different things.

      To *initialize* the value of `test' you'd write:

      int main(void) {
      struct add test = {{{3}}};
      return 0;
      }

      HTH,
      --ag


      --
      Artie Gold -- Austin, Texas
      http://goldsays.blogspot.com (new post 8/5)
      http://www.cafepress.com/goldsays
      "If you have nothing to hide, you're not trying!"

      Comment

      • Chad

        #4
        Re: How to initialize and array in a structure?


        Artie Gold wrote:[color=blue]
        > Chad wrote:[color=green]
        > > How do I set the value of a[0] = 3 in the following lines of code?
        > >
        > > #include <stdio.h>
        > >
        > > struct letter{
        > > char a[0];[/color]
        > An array with a length of 0 is not permitted in C. ITYM:
        > char a[1];
        > (Which doesn't make a whole lot of sense either.)[color=green]
        > > };
        > >
        > > struct add {
        > > struct letter addit;
        > > };
        > >
        > > int main(void) {
        > > struct add test;[/color]
        > /* assignment */
        > test.addit.a[0] = 3;[color=green]
        > > return 0;
        > > }
        > >
        > > when I try something like test.add=3 or test.addit.a=3, the compiler
        > > gives me the warning
        > > 'incompatible types in assignment'.
        > >[/color]
        > Of course, you need to realize that assignment and initialization are
        > two very different things.
        >
        > To *initialize* the value of `test' you'd write:
        >
        > int main(void) {
        > struct add test = {{{3}}};
        > return 0;
        > }
        >
        > HTH,
        > --ag
        >
        >
        > --
        > Artie Gold -- Austin, Texas
        > http://goldsays.blogspot.com (new post 8/5)
        > http://www.cafepress.com/goldsays
        > "If you have nothing to hide, you're not trying!"[/color]

        Okay, I think I got this. If not, I'll be back on here sometime
        tommorrow asking more questions.

        Comment

        • Ravi Uday

          #5
          Re: How to initialize and array in a structure?



          Artie Gold wrote:
          [color=blue]
          > Chad wrote:
          >[color=green]
          >> How do I set the value of a[0] = 3 in the following lines of code?
          >>
          >> #include <stdio.h>
          >>
          >> struct letter{
          >> char a[0];[/color]
          >
          > An array with a length of 0 is not permitted in C.[/color]

          I think that type when declared as a last member of a struct element
          can be used for setting right the alignment issues w.r.t structures.

          - Ravi


          ITYM:[color=blue]
          > char a[1];
          > (Which doesn't make a whole lot of sense either.)
          >[color=green]
          >> };
          >>
          >> struct add {
          >> struct letter addit;
          >> };
          >>
          >> int main(void) {
          >> struct add test;[/color]
          >
          > /* assignment */
          > test.addit.a[0] = 3;
          >[color=green]
          >> return 0;
          >> }
          >>
          >> when I try something like test.add=3 or test.addit.a=3, the compiler
          >> gives me the warning
          >> 'incompatible types in assignment'.
          >>[/color]
          > Of course, you need to realize that assignment and initialization are
          > two very different things.
          >
          > To *initialize* the value of `test' you'd write:
          >
          > int main(void) {
          > struct add test = {{{3}}};
          > return 0;
          > }
          >
          > HTH,
          > --ag
          >
          >[/color]

          Comment

          • Jordan Abel

            #6
            Re: How to initialize and array in a structure?

            On 2005-12-10, Ravi Uday <raviuday@gmail .com> wrote:[color=blue]
            >
            >
            > Artie Gold wrote:
            >[color=green]
            >> Chad wrote:
            >>[color=darkred]
            >>> How do I set the value of a[0] = 3 in the following lines of code?
            >>>
            >>> #include <stdio.h>
            >>>
            >>> struct letter{
            >>> char a[0];[/color]
            >>
            >> An array with a length of 0 is not permitted in C.[/color]
            >
            > I think that type when declared as a last member of a struct element
            > can be used for setting right the alignment issues w.r.t structures.[/color]

            What issues?

            Comment

            • Mark McIntyre

              #7
              Re: How to initialize and array in a structure?

              On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
              <cdalten@gmail. com> wrote:
              [color=blue]
              >How do I set the value of a[0] = 3 in the following lines of code?
              >
              >#include <stdio.h>
              >
              >struct letter{
              > char a[0];[/color]

              char a[1];

              you can't have a zero size array. Think about it...
              [color=blue]
              >when I try something like test.add=3 or test.addit.a=3, the compiler
              >gives me the warning >'incompatibl e types in assignment'.[/color]

              a is of type char[], 3 is of type int, they're incompatible.

              test.letter.a[0] = 3;
              would work, but set a[0] to the numeric value 3, not the character
              value '3'. I imagine you want the latter given your object names.

              The simplest way to do that is '0'+3, since the number characters are
              guaranteed to be in ascending order, and '0' is the first one.

              [color=blue]
              >
              >Thanks in advance
              >Chad[/color]

              ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

              Comment

              • Artie Gold

                #8
                Re: How to initialize and array in a structure?

                Ravi Uday wrote:[color=blue]
                >
                >
                > Artie Gold wrote:
                >[color=green]
                >> Chad wrote:
                >>[color=darkred]
                >>> How do I set the value of a[0] = 3 in the following lines of code?
                >>>
                >>> #include <stdio.h>
                >>>
                >>> struct letter{
                >>> char a[0];[/color]
                >>
                >>
                >> An array with a length of 0 is not permitted in C.[/color]
                >
                >
                > I think that type when declared as a last member of a struct element
                > can be used for setting right the alignment issues w.r.t structures.
                >
                > - Ravi[/color]

                Ummm, no.

                You are thinking of one of two things:

                1) The non-standard (but frequently encountered) `struct hack' wherein
                an array of one element is placed at the end of a struct declaration and
                the the actual size is determined by how much memory is allocated to the
                struct.

                2) The C99 flexible struct where the last member of the struct has *no*
                size (i.e. []) -- effectively a `blessed struct hack'.

                HTH,
                --ag[color=blue]
                >[/color]
                [snip]

                --
                Artie Gold -- Austin, Texas
                http://goldsays.blogspot.com (new post 8/5)
                http://www.cafepress.com/goldsays
                "If you have nothing to hide, you're not trying!"

                Comment

                • Keith Thompson

                  #9
                  Re: How to initialize and array in a structure?

                  Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                  > On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
                  > <cdalten@gmail. com> wrote:
                  >[color=green]
                  >>How do I set the value of a[0] = 3 in the following lines of code?
                  >>
                  >>#include <stdio.h>
                  >>
                  >>struct letter{
                  >> char a[0];[/color]
                  >
                  > char a[1];
                  >
                  > you can't have a zero size array. Think about it...[/color]
                  [...]

                  How will thinking about it help? C could have allowed zero size
                  arrays with consistent semantics; other languages do so.

                  C doesn't allow zero size arrays because the standard says so.

                  --
                  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

                  • Mark McIntyre

                    #10
                    Re: How to initialize and array in a structure?

                    On Sat, 10 Dec 2005 21:16:17 GMT, in comp.lang.c , Keith Thompson
                    <kst-u@mib.org> wrote:
                    [color=blue]
                    >Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
                    >> On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
                    >> <cdalten@gmail. com> wrote:
                    >>[/color][/color]
                    (reordered slightly to make my reply easier to type)
                    [color=blue][color=green]
                    >> you can't have a zero size array. Think about it...[/color]
                    >[...]
                    >
                    >How will thinking about it help?[/color]

                    The OP wants to store something in it.How much can you store in a
                    zero-size array? This is the thinking I'd have expected someone to
                    do.
                    [color=blue]
                    >C could have allowed zero size
                    >arrays with consistent semantics; other languages do so.[/color]

                    I can't help what other languages might do, and its also offtopic
                    here...

                    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                    Comment

                    • Keith Thompson

                      #11
                      Re: How to initialize and array in a structure?

                      Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                      > On Sat, 10 Dec 2005 21:16:17 GMT, in comp.lang.c , Keith Thompson
                      > <kst-u@mib.org> wrote:
                      >[color=green]
                      >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
                      >>> On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
                      >>> <cdalten@gmail. com> wrote:
                      >>>[/color][/color]
                      > (reordered slightly to make my reply easier to type)
                      >[color=green][color=darkred]
                      >>> you can't have a zero size array. Think about it...[/color]
                      >>[...]
                      >>
                      >>How will thinking about it help?[/color]
                      >
                      > The OP wants to store something in it.How much can you store in a
                      > zero-size array? This is the thinking I'd have expected someone to
                      > do.[/color]

                      How many elements can you store in an array of size 1? 1.

                      How many elements can you store in an array of size 0? 0.

                      If the size of the array is computed, either at compilation time or,
                      for VLAs, at run time, excluding empty arrays is a special case that
                      doesn't really need to be there.

                      Except that, since C doesn't require bounds checking, it can sometimes
                      be possible to store elements past the declared end of the array. The
                      more portable (in C90) version of the struct hack uses a 1-element
                      aray; if 0-element arrays were supported (as they are by some
                      compilers), they'd be a slightly more elegant way to implement it.
                      [color=blue][color=green]
                      >>C could have allowed zero size
                      >>arrays with consistent semantics; other languages do so.[/color]
                      >
                      > I can't help what other languages might do, and its also offtopic
                      > here...[/color]

                      It's a response to your claim that thinking about zero-sized arrays
                      inevitably leads to the conclusion that they shouldn't be allowed. It
                      doesn't, and plenty of smart people have reached the opposite
                      conclusion.

                      I'm not necessarily arguing that C *should* support zero-length
                      arrays, just that it's not obvious that it shouldn't.

                      --
                      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

                      • Malcolm

                        #12
                        Re: How to initialize and array in a structure?


                        "Keith Thompson" <kst-u@mib.org> wrote[color=blue]
                        > I'm not necessarily arguing that C *should* support zero-length
                        > arrays, just that it's not obvious that it shouldn't.
                        >[/color]
                        In a sense it does.
                        We can allocate a zero-length array using malloc, iterate over it zero
                        times with a zero number of derefrences, and then free it.


                        Comment

                        • pete

                          #13
                          Re: How to initialize and array in a structure?

                          Malcolm wrote:[color=blue]
                          >
                          > "Keith Thompson" <kst-u@mib.org> wrote[color=green]
                          > > I'm not necessarily arguing that C *should* support zero-length
                          > > arrays, just that it's not obvious that it shouldn't.
                          > >[/color]
                          > In a sense it does.
                          > We can allocate a zero-length array using malloc,
                          > iterate over it zero
                          > times with a zero number of derefrences, and then free it.[/color]

                          I don't like the idea of zero length arrays,
                          but Douglas A. Gwyn, does, or did at one time.



                          --
                          pete

                          Comment

                          • Keith Thompson

                            #14
                            Re: How to initialize and array in a structure?

                            "Malcolm" <regniztar@btin ternet.com> writes:[color=blue]
                            > "Keith Thompson" <kst-u@mib.org> wrote[color=green]
                            >> I'm not necessarily arguing that C *should* support zero-length
                            >> arrays, just that it's not obvious that it shouldn't.
                            >>[/color]
                            > In a sense it does.
                            > We can allocate a zero-length array using malloc, iterate over it zero
                            > times with a zero number of derefrences, and then free it.[/color]

                            Yes, but since malloc(0) can return either a valid pointer or NULL, we
                            can't safely use pointer arithmetic to construct a pointer value just
                            past the end of the array. Zero is a (gratuitous, IMHO) special case
                            even for malloc().

                            --
                            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

                            • Realos Osis

                              #15
                              Re: How to initialize and array in a structure?

                              [color=blue]
                              > char a[1];[/color]

                              Well, I can't imagine a reasonable use of such an array definition. Consider the
                              following code:



                              #include <stdio.h>

                              int main(void)
                              {
                              char szstring[1]={'\0'};
                              int result;

                              result=scanf("% s",szstring);ff lush(stdin);
                              printf("number of characters read: %i\n",result);

                              printf("%s\n",s zstring);
                              printf("next memory place: %c\n",*(szstrin g+1));

                              }


                              scanf does not check memory bounds and thus ruthlessly overwrites the next
                              memory block. fgets would do the job here by not allowing you to enter enything
                              at all in the string for an ascii nul is the last (in our case the only)
                              character in a string.
                              [color=blue]
                              >
                              > test.letter.a[0] = 3;
                              > would work, but set a[0] to the numeric value 3, not the character
                              > value '3'. I imagine you want the latter given your object names.[/color]
                              [color=blue]
                              >
                              > The simplest way to do that is '0'+3, since the number characters are
                              > guaranteed to be in ascending order, and '0' is the first one.
                              >[/color]


                              Can you please elaborate on what is the difference between '0'+3 and 3?

                              I tried following

                              printf("'0'+3: %d\n",(szstring[0]='0'+3));
                              printf("'0'+3: %c\n",(szstring[0]='0'+3));

                              I printed:
                              '0'+3: 51
                              '0'+3: 3


                              regards,

                              Realos







                              Comment

                              Working...