assigning values to a struct

Discussion in 'C Programming' started by Chris Hiler, Aug 26, 2004.

  1. Chris Hiler

    Chris Hiler Guest

    Why isn't it possible to assign a value to a struct at run time?

    In my header file I have something like this:

    typedef struct SomeStruct_t
    {
    int Member1;
    int Member2;
    int Member3;
    };


    then in main.c I declare somthing like this:

    struct SomeStruct_t ExampleStructure;


    Then in main all I want to do is this:

    void main(void)
    {
    ExampleStructure.Member1 = 1;

    }


    My compiler tells me that '=' is an incompatible operator. It might
    help to know that I'm using a keil 8051 C compiler, and it isn't
    totally ANSI-C compliant. If someone could let me know what I'm doing
    wrong or give me some guidance I'd really appreciate it.
    Thanks,
    Chris
     
    Chris Hiler, Aug 26, 2004
    #1
    1. Advertising

  2. (Chris Hiler) wrote in
    news::

    > Why isn't it possible to assign a value to a struct at run time?
    >
    > In my header file I have something like this:
    >
    > typedef struct SomeStruct_t
    > {
    > int Member1;
    > int Member2;
    > int Member3;
    > };


    You forgot the typedef! It goes between the closing } and the ; and can be
    the same name as the struct tag (which you call SomeStruct_t). Either drop
    the work typedef or add a typedef name.

    > then in main.c I declare somthing like this:


    This is outside of main(), why?

    > struct SomeStruct_t ExampleStructure;
    >
    >
    > Then in main all I want to do is this:
    >
    > void main(void)
    > {
    > ExampleStructure.Member1 = 1;


    Fix the typedef and you should be fine.
    > }
    >


    --
    - Mark ->
    --
     
    Mark A. Odell, Aug 26, 2004
    #2
    1. Advertising

  3. Chris Hiler wrote:

    > Why isn't it possible to assign a value to a struct at run time?
    >
    > In my header file I have something like this:
    >
    > typedef struct SomeStruct_t
    > {
    > int Member1;
    > int Member2;
    > int Member3;
    > };
    >
    >
    > then in main.c I declare somthing like this:
    >
    > struct SomeStruct_t ExampleStructure;
    >
    >
    > Then in main all I want to do is this:
    >
    > void main(void)
    > {
    > ExampleStructure.Member1 = 1;
    >
    > }
    >
    >
    > My compiler tells me that '=' is an incompatible operator. It might
    > help to know that I'm using a keil 8051 C compiler, and it isn't
    > totally ANSI-C compliant.


    Big deal. It is your useless 'typedef' that is the problem:



    /* mha: The original typedef was comletely senseless. It provided no
    new type name. An analogous typedef would be 'typedef int;'. I
    have remove the useless keyword 'typedef', since the attempted use
    doesn't need it. */

    struct SomeStruct_t
    {
    int Member1;
    int Member2;
    int Member3;
    };

    /* mha: The original return type for main was 'void'. This is
    illegal, illiterate, and proves that the original poster has
    violated civilized norms by neither following the newsgroup nor
    checking the FAQ before posting. Fixed. */

    int main(void)
    {
    struct SomeStruct_t ExampleStructure;
    ExampleStructure.Member1 = 1;
    return 0; /* mha: for C89 or C90 */
    }
     
    Martin Ambuhl, Aug 26, 2004
    #3
  4. Chris Hiler

    Default User Guest

    "Mark A. Odell" wrote:
    >
    > (Chris Hiler) wrote in
    > news::
    >
    > > Why isn't it possible to assign a value to a struct at run time?
    > >
    > > In my header file I have something like this:
    > >
    > > typedef struct SomeStruct_t
    > > {
    > > int Member1;
    > > int Member2;
    > > int Member3;
    > > };

    >
    > You forgot the typedef! It goes between the closing } and the ; and can be
    > the same name as the struct tag (which you call SomeStruct_t). Either drop
    > the work typedef or add a typedef name.
    >
    > > then in main.c I declare somthing like this:

    >
    > This is outside of main(), why?
    >
    > > struct SomeStruct_t ExampleStructure;
    > >
    > >
    > > Then in main all I want to do is this:
    > >
    > > void main(void)
    > > {
    > > ExampleStructure.Member1 = 1;

    >
    > Fix the typedef and you should be fine.



    I don't see why. The typedef is useless because he didn't actually
    create a type, but so what? The struct declaration itself should ok.
    When he declared the variable ExampleStructure he used the struct
    keyword in the declaration, he wasn't relying on a typedef. I doubt
    you'd get that diagnostic anyway.



    Brian Rodenborn
     
    Default User, Aug 26, 2004
    #4
  5. Chris Hiler

    Jack Klein Guest

    On Thu, 26 Aug 2004 14:30:43 -0400, Martin Ambuhl
    <> wrote in comp.lang.c:

    [snip]

    > /* mha: The original return type for main was 'void'. This is
    > illegal, illiterate, and proves that the original poster has
    > violated civilized norms by neither following the newsgroup nor
    > checking the FAQ before posting. Fixed. */


    Actually not in this particular case. Keil C for the 'C51
    architecture is absolutely, positively, beyond a doubt a freestanding,
    not a hosted, environment.

    In which case, as the standard states:

    ========
    5.1.2.1 Freestanding environment

    1 In a freestanding environment (in which C program execution may take
    place without any benefit of an operating system), the name and type
    of the function called at program startup are implementation-defined.
    Any library facilities available to a freestanding program, other than
    the minimal set required by clause 4, are implementation-defined.

    2 The effect of program termination in a freestanding environment is
    implementation-defined.
    ========

    Keil is quite within their rights as defining the startup function for
    their implementation as "void main(void)", which makes a lot of sense
    when you consider that main() is NOT EVER intended to return. In an
    embedded system there is nothing to return to, and such an event would
    be a disaster and a complete failure.

    So easy with the "hosted environment chauvinism", OK? ;)

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 27, 2004
    #5
  6. Jack Klein <> writes:
    [...]
    > Actually not in this particular case. Keil C for the 'C51
    > architecture is absolutely, positively, beyond a doubt a freestanding,
    > not a hosted, environment.
    >
    > In which case, as the standard states:
    >
    > ========
    > 5.1.2.1 Freestanding environment
    >
    > 1 In a freestanding environment (in which C program execution may take
    > place without any benefit of an operating system), the name and type
    > of the function called at program startup are implementation-defined.

    [...]
    > ========
    >
    > Keil is quite within their rights as defining the startup function for
    > their implementation as "void main(void)", which makes a lot of sense
    > when you consider that main() is NOT EVER intended to return. In an
    > embedded system there is nothing to return to, and such an event would
    > be a disaster and a complete failure.


    Note that "implementation-defined" means that they're required do
    document it. Does Keil's documentation specifically recommend
    void main(void)? (I don't mean to imply that they don't, I'm just
    curious.)

    If it is documented, and if I ever had an opportunity to use Keil C,
    I'd use void main(void) -- but I'd wash my hands afterward. :cool:}

    --
    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, Aug 27, 2004
    #6
  7. Chris Hiler

    Old Wolf Guest

    Martin Ambuhl <> wrote:
    > Chris Hiler wrote:
    >
    > It might help to know that I'm using a keil 8051 C compiler


    >
    > /* mha: The original return type for main was 'void'. This is
    > illegal, illiterate, and proves that the original poster has
    > violated civilized norms by neither following the newsgroup nor
    > checking the FAQ before posting. Fixed. */
    >
    > int main(void)


    The OP indicated that he was using a freestanding implementation,
    so void main(void) is OK. In fact one such system I use includes
    it as a prototype in the system headers.
     
    Old Wolf, Aug 27, 2004
    #7
  8. Old Wolf wrote:
    > Martin Ambuhl <> wrote:
    >
    >>Chris Hiler wrote:
    >>
    >>It might help to know that I'm using a keil 8051 C compiler

    >
    >
    >> /* mha: The original return type for main was 'void'. This is
    >> illegal, illiterate, and proves that the original poster has
    >> violated civilized norms by neither following the newsgroup nor
    >> checking the FAQ before posting. Fixed. */
    >>
    >>int main(void)

    >
    >
    > The OP indicated that he was using a freestanding implementation,
    > so void main(void) is OK. In fact one such system I use includes
    > it as a prototype in the system headers.


    The OP indicated that he was using "a keil 8051 C compiler." Those
    words may mean to you that "he was using a freestanding implementation,"
    but those words appear nowhere in his message. The normal assumption in
    the newsgroup is that the language as defined for a hosted environment
    is being discussed. To assume otherwise is to through huge chunks of
    the language out the window. No one should be required to learn the
    names of all the freestanding implementations. Call MS's C
    implementation for Windows "freestanding" and we can have fun with the
    floodgates that opens.
     
    Martin Ambuhl, Aug 27, 2004
    #8
  9. remove the "typedef".

    chok

    Chris Hiler wrote:

    > Why isn't it possible to assign a value to a struct at run time?
    >
    > In my header file I have something like this:
    >
    > typedef struct SomeStruct_t
    > {
    > int Member1;
    > int Member2;
    > int Member3;
    > };
    >
    >
    > then in main.c I declare somthing like this:
    >
    > struct SomeStruct_t ExampleStructure;
    >
    >
    > Then in main all I want to do is this:
    >
    > void main(void)
    > {
    > ExampleStructure.Member1 = 1;
    >
    > }
    >
    >
    > My compiler tells me that '=' is an incompatible operator. It might
    > help to know that I'm using a keil 8051 C compiler, and it isn't
    > totally ANSI-C compliant. If someone could let me know what I'm doing
    > wrong or give me some guidance I'd really appreciate it.
    > Thanks,
    > Chris
     
    ChokSheak Lau, Aug 27, 2004
    #9
  10. Martin Ambuhl <> writes:
    [...]
    > The OP indicated that he was using "a keil 8051 C compiler." Those
    > words may mean to you that "he was using a freestanding
    > implementation," but those words appear nowhere in his message.

    [...]

    If you happen to know what an 8051 is, you can probably assume that
    it's not likely to support a hosted C implementation.

    Those who know what an 8051 is, don't be too hard on those who don't.
    Those who don't, don't be too hard on those who assumed that you would.

    --
    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, Aug 29, 2004
    #10
    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. Johnsy Joseph
    Replies:
    4
    Views:
    4,452
    Jim Lewis
    Sep 21, 2004
  2. Guest

    assigning values to form inputs

    Guest, Aug 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    538
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,440
    Chris Fogelklou
    Apr 20, 2004
  4. Cliff
    Replies:
    23
    Views:
    1,082
    Jerry Coffin
    Sep 5, 2007
  5. weston
    Replies:
    1
    Views:
    278
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page