assigning values to a struct

C

Chris Hiler

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
 
M

Mark A. Odell

(e-mail address removed) (Chris Hiler) wrote in

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

Martin Ambuhl

Chris said:
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 */
}
 
D

Default User

Mark A. Odell said:
(e-mail address removed) (Chris Hiler) wrote in


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.


This is outside of main(), why?


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
 
J

Jack Klein

On Thu, 26 Aug 2004 14:30:43 -0400, Martin Ambuhl

[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? ;)
 
K

Keith Thompson

Jack Klein said:
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:}
 
O

Old Wolf

Martin Ambuhl said:
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.
 
M

Martin Ambuhl

Old said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top