Assignment to structures

G

Gary Schnabl

What might the problems be in the following code?:

The problems occur at these two lines:
<snippage>
p.L2 = {5,6,10,12,255};
<snippage>
q={{0,0,5,6,128},{5,6,-10,6,128},1};
<...>

The error messages are presented after the source code.

Gary Schnabl
Detroit

****************************************************

#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12c32"


void Setp(void);

struct theline {
int x1,y1; // starting point
int x2,y2; // starting point
char color; // color
};
typedef struct theline line;

struct thepath {
line L1,L2; // two lines
char direction;
};
typedef struct thepath path;
path p; // global


void Setp(void) { line myLine; path q;
p.L1.x1=5; // black line from 5,6 to 10,12
p.L1.y1=6;
p.L1.x2=10;
p.L1.y2=12;
p.L1.color=255;
p.L2 = {5,6,10,12,255};
// black line from 5,6 to 10,12
p.direction=-1;
myLine=p.L1;
q={{0,0,5,6,128},{5,6,-10,6,128},1};
q=p;
};


void main(void) {
/* put your own code here */
EnableInterrupts;

for(;;) {} /* wait forever */
}


***************************************************************
Error messages:

Error : C2450: Expected: . * + - & ! ~ ++ -- -> [ ( IDENT CONSTANT
STRING sizeof __alignof__ __va_sizeof__ __va_arg_type__

main.c line 30

Error : C1822: Type mismatch (expected 'theline ', given 'error ')

main.c line 30

Error : C1806: Illegal cast-operation

main.c line 30

Error : C2801: ';' missing

main.c line 30

Error : C2450: Expected: . * + - & ! ~ ++ -- -> [ ( IDENT CONSTANT
STRING sizeof __alignof__ __va_sizeof__ __va_arg_type__

main.c line 34

Error : C1822: Type mismatch (expected 'thepath ', given 'error ')

main.c line 34

Error : C1806: Illegal cast-operation

main.c line 34

Error : C2801: ';' missing

main.c line 34

Error : C2801: '}' missing

main.c line 34

Error : C2801: ';' missing

main.c line 34

Error : C2801: '}' missing

main.c line 34

Error : Compile failed
 
M

Martin Ambuhl

Gary said:
What might the problems be in the following code?:

The OP's code is after the corrected code, which requires C99.
[Corrected code]
#if 0
/* mha: Two erroneous inclusions of non-standard headers which are
also not provided. */
#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

/* mha: ignored pragma */
#pragma LINK_INFO DERIVATIVE "mc9s12c32"
#endif

void Setp(void);

struct theline
{
int x1, y1; /* starting point */
int x2, y2; /* starting point */
unsigned char color; /* mha: since char may be signed, this
is changed to unsigned so the
attempted assignment of 255 to it
does not cause a problem. */
};
typedef struct theline line;

struct thepath
{
line L1, L2; /* two lines */
char direction;
};
typedef struct thepath path;
path p; /* global */


void Setp(void)
{
line myLine;
path q;
p.L1.x1 = 5; /* black line from 5,6 to 10,12 */
p.L1.y1 = 6;
p.L1.x2 = 10;
p.L1.y2 = 12;
p.L1.color = 255;
p.L2 = (line) {
5, 6, 10, 12, 255}; /* mha: fixed RHS; still illegal if not
C99 */
/* black line from 5,6 to 10,12 */
p.direction = -1;
myLine = p.L1;
q = (path) { {
0, 0, 5, 6, 128}, {
5, 6, -10, 6, 128}, 1}; /* mha: fixed RHS; still illegal if not
C99 */
q = p;
} /* mha: removed illegal ';' */


int /* mha: fixed illegal 'void' */ main(void)
{
/* put your own code here */
#if 0
/* mha: 'EnableInterrupts' is an undeclared identifier */
EnableInterrupts;
#endif

for (;;) {
} /* wait forever */
return 0; /* mha: a good idea even if C99 lets us
get away without it. */
}


[OP's code]
 
A

Artie Gold

Gary said:
What might the problems be in the following code?:

The problems occur at these two lines:
<snippage>
p.L2 = {5,6,10,12,255};
<snippage>
q={{0,0,5,6,128},{5,6,-10,6,128},1};
<...>

The error messages are presented after the source code.

Gary Schnabl
Detroit

****************************************************

#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

Non-standard headers.
#pragma LINK_INFO DERIVATIVE "mc9s12c32"
Non-standard pragma

void Setp(void);

struct theline {
int x1,y1; // starting point
int x2,y2; // starting point
char color; // color
};
typedef struct theline line;

struct thepath {
line L1,L2; // two lines
char direction;
};
typedef struct thepath path;
path p; // global


void Setp(void) { line myLine; path q;
p.L1.x1=5; // black line from 5,6 to 10,12
p.L1.y1=6;
p.L1.x2=10;
p.L1.y2=12;
p.L1.color=255;
p.L2 = {5,6,10,12,255};
What is this supposed to be? You can use a form (somewhat) like this to
*initialize* a structure, but not in an assignment.
// black line from 5,6 to 10,12
p.direction=-1;
myLine=p.L1;
q={{0,0,5,6,128},{5,6,-10,6,128},1};

So let me get this straight...you're trying -- unsuccessfully, I might
add -- to assign something to `q' (which, itself, is local to a function
returning void)...

And then immediately *reassigning* `q'...which goes away when the
function exits anyway.
};


void main(void) {

`main' returns `int'.
/* put your own code here */
EnableInterrupts;

Non-standard function.
for(;;) {} /* wait forever */
}

What are you trying to do here? Do you have a (decent) book that covers
standard C? Have you read the FAQ?

Now mind you, you could *initialize* `p' like this:

struct thepath p = {{{5, 6}, {10, 12}, 255},
{{5, 6}, {10, 12}, 255},
1};

Get a book. Read the FAQ.
And by all means try again.

HTH,
--ag
 
J

Jack Klein

What might the problems be in the following code?:

The problems occur at these two lines:
<snippage>
p.L2 = {5,6,10,12,255};
<snippage>
q={{0,0,5,6,128},{5,6,-10,6,128},1};
<...>

The error messages are presented after the source code.

Gary Schnabl
Detroit

****************************************************

#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12c32"


void Setp(void);

struct theline {
int x1,y1; // starting point
int x2,y2; // starting point
char color; // color
};
typedef struct theline line;

struct thepath {
line L1,L2; // two lines
char direction;
};
typedef struct thepath path;
path p; // global


void Setp(void) { line myLine; path q;
p.L1.x1=5; // black line from 5,6 to 10,12
p.L1.y1=6;
p.L1.x2=10;
p.L1.y2=12;
p.L1.color=255;
p.L2 = {5,6,10,12,255};

p.L2 is a theLine structure. You cannot assign a collection of
multiple values to a structure in this way. You can only assign
another structure of the same type to a structure. That can be from
another structure object of the same type, or if you have a C99
compiler, a struct literal.

But the code, as written above, is just plain not legal C. You need
to assign to the individual elements of p.L2 just as you did to p.L1.
// black line from 5,6 to 10,12
p.direction=-1;
myLine=p.L1;
q={{0,0,5,6,128},{5,6,-10,6,128},1};

The same thing here. You can use this syntax to initialize a
structure in the line where you define it, but not to assign to it.

I'd suggest you get a book on C if you don't have one already. The
second edition of "The C Programming Language" by Kernighan and
Ritchie is excellent.
 
G

Gary Schnabl

Artie Gold said:
Non-standard headers.
What is this supposed to be? You can use a form (somewhat) like this to
*initialize* a structure, but not in an assignment.


So let me get this straight...you're trying -- unsuccessfully, I might
add -- to assign something to `q' (which, itself, is local to a function
returning void)...

And then immediately *reassigning* `q'...which goes away when the
function exits anyway.

`main' returns `int'.


Non-standard function.

What are you trying to do here? Do you have a (decent) book that covers
standard C? Have you read the FAQ?

Now mind you, you could *initialize* `p' like this:

struct thepath p = {{{5, 6}, {10, 12}, 255},
{{5, 6}, {10, 12}, 255},
1};

Get a book. Read the FAQ.
And by all means try again.

HTH,
--ag



I'm not claiming that this would be portable ANSI-C code. This code snippet
was described in Professor Jonathan Valvano's tutorial (at the U of TX in
Austin) to be used with the CodeWarrior IDE for developing firmware for the
Freescale Semiconductor HC11 or HC12 embedded microprocessors.
http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#ACCESS

The headers used are those that CodeWarrior uses for a particular CPU
derivative - the Freescale Semiconductor MC9S12C32 in this case - and the
hidef.h is the header that probably originated with the (German?) Hiware
software that Metrowerks acquired a few years ago.

I wasn't attempting to initialize the structure or its members, but assign
to them. I'm simply following some code that was in the tutorial I mentioned
in the preceding paragraph. I know the difference between local and global
variables and their scopes. Perhaps there could have been further code later
included in the function for using q. I didn't author the example in the
tutorial, but am simply trying to test it with my CodeWarrior version.

However, my IDE would not accept Professor Valvano's code, as written. C
used for developing firmware for embedded microprocessors may or will
deviate from ANSI-C in some respects through its optional configurations.
I'm using the CodeWarrior factory defaults, which generally cause few
problems with ANSI-C, as far as I knew.

Since this won't properly compile, perhaps you should take this up with
Professor Valvano, as you both reside in or near Austin.


Gary Schnabl
(Southwest) Detroit
 
J

Jack Klein

I'm not claiming that this would be portable ANSI-C code. This code snippet
was described in Professor Jonathan Valvano's tutorial (at the U of TX in
Austin) to be used with the CodeWarrior IDE for developing firmware for the
Freescale Semiconductor HC11 or HC12 embedded microprocessors.
http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#ACCESS

The headers used are those that CodeWarrior uses for a particular CPU
derivative - the Freescale Semiconductor MC9S12C32 in this case - and the
hidef.h is the header that probably originated with the (German?) Hiware
software that Metrowerks acquired a few years ago.

I wasn't attempting to initialize the structure or its members, but assign
to them. I'm simply following some code that was in the tutorial I mentioned
in the preceding paragraph. I know the difference between local and global
variables and their scopes. Perhaps there could have been further code later
included in the function for using q. I didn't author the example in the
tutorial, but am simply trying to test it with my CodeWarrior version.

However, my IDE would not accept Professor Valvano's code, as written. C
used for developing firmware for embedded microprocessors may or will
deviate from ANSI-C in some respects through its optional configurations.
I'm using the CodeWarrior factory defaults, which generally cause few
problems with ANSI-C, as far as I knew.

Since this won't properly compile, perhaps you should take this up with
Professor Valvano, as you both reside in or near Austin.

No, perhaps YOU should take it up with the professor. His code is
garbage, and I don't mean the embedded platform specific headers. I
mean lines like:

p.L2 = {5,6,10,12,255};

....and:

q={{0,0,5,6,128},{5,6,-10,6,128},1};

This is just plain not C, in any way, shape or form. I've used
CodeWarrior for the HS12 family, and it never has accepted anything
like this.

You should suggest to the professor that he actually verify that code
compiles before he includes bullSchildt in a lecture, article, or
book.

The code does not compile because it is not valid C code, not for ISO
C, not for K&R C, not for a hosted environment, not for an embedded
system.

Your C compiler won't compile COBOL either.
 
G

Gary Schnabl

I'll top post this so as not to get lost in the code.

(1) The nonstandard headers are used for a specific purpose which may not
always be portable C for developing firmware for embedded microprocessors.

(2) The for (;;) at the end is there so that the program never terminates
for the embedded microprocessor app. Some program code usually could be
installed there by a firmware developer. These projects may also have a
number of such loops with some means to exit them.

(3) The (plain) 8-bit char by (compiler) default will be unsigned char for
use by the Metrowerks Codewarrior (Hiware) compiler for these chips - the
Motorola (Freescale Semiconductor) HC08, HC11, or HC(S)12. The non-standard
headers have such #defines such as #define unsigned char byte and #define
unsigned int word, and also include some of the common standard C header
files. These headers should have no effect on the problem at hand - the
assignments to the structures. This code came from a tutorial written for
use with some Motorola embedded microprocessor chip series - the HC11 or
HC(S)12. Confer another reply I made for any specifics on this.
http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#ACCESS

(4) Of course, the pragma is specific to the Hiware (CodeWarrior) compiler.

(5) I made the two casts that you cited myself earlier, in addition to
deleting the extra semicolon after the definition of Setp() in running my
own version of this code before I posted this request. BTW, I copied the
code directly from the tutorial.

(6) Perhaps, there are some options with CodeWarrior that I need to consider
before this code snippet will run. I wanted to see if I could get it
straightened on this newsgroup before I addressed Metrowerks or Professor
Valvano (U of TX) on this issue.

(7) EnableInterrupts is a macro for some inline assembly which resets a bit
in a register on the HC12 chips aftert the chip is reset.



Tnx,
GS


Gary said:
What might the problems be in the following code?:

The OP's code is after the corrected code, which requires C99.
[Corrected code]
#if 0
/* mha: Two erroneous inclusions of non-standard headers which are
also not provided. */
#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

/* mha: ignored pragma */
#pragma LINK_INFO DERIVATIVE "mc9s12c32"
#endif

void Setp(void);

struct theline
{
int x1, y1; /* starting point */
int x2, y2; /* starting point */
unsigned char color; /* mha: since char may be signed, this
is changed to unsigned so the
attempted assignment of 255 to it
does not cause a problem. */
};
typedef struct theline line;

struct thepath
{
line L1, L2; /* two lines */
char direction;
};
typedef struct thepath path;
path p; /* global */


void Setp(void)
{
line myLine;
path q;
p.L1.x1 = 5; /* black line from 5,6 to 10,12 */
p.L1.y1 = 6;
p.L1.x2 = 10;
p.L1.y2 = 12;
p.L1.color = 255;
p.L2 = (line) {
5, 6, 10, 12, 255}; /* mha: fixed RHS; still illegal if not
C99 */
/* black line from 5,6 to 10,12 */
p.direction = -1;
myLine = p.L1;
q = (path) { {
0, 0, 5, 6, 128}, {
5, 6, -10, 6, 128}, 1}; /* mha: fixed RHS; still illegal if not
C99 */
q = p;
} /* mha: removed illegal ';' */


int /* mha: fixed illegal 'void' */ main(void)
{
/* put your own code here */
#if 0
/* mha: 'EnableInterrupts' is an undeclared identifier */
EnableInterrupts;
#endif

for (;;) {
} /* wait forever */
return 0; /* mha: a good idea even if C99 lets us
get away without it. */
}


[OP's code]

#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12c32"


void Setp(void);

struct theline {
int x1,y1; // starting point
int x2,y2; // starting point
char color; // color
};
typedef struct theline line;

struct thepath {
line L1,L2; // two lines
char direction;
};
typedef struct thepath path;
path p; // global


void Setp(void) { line myLine; path q;
p.L1.x1=5; // black line from 5,6 to 10,12
p.L1.y1=6;
p.L1.x2=10;
p.L1.y2=12;
p.L1.color=255;
p.L2 = {5,6,10,12,255};
// black line from 5,6 to 10,12
p.direction=-1;
myLine=p.L1;
q={{0,0,5,6,128},{5,6,-10,6,128},1};
q=p;
};


void main(void) {
/* put your own code here */
EnableInterrupts;

for(;;) {} /* wait forever */
}
 
G

Gary Schnabl

Now mind you, you could *initialize* `p' like this:

struct thepath p = {{{5, 6}, {10, 12}, 255},
{{5, 6}, {10, 12}, 255},
1};

Get a book. Read the FAQ.
And by all means try again.

HTH,
--ag



Since I noticed other errors in the tutorial, I took it upon myself tonight
to check some more of Prof. Valvano's code, and that code example just
happened to be the first one I decided to check. (I didn't start at the
beginning of the tutorial and instead arbitrarily chose Chapter 9.) I didn't
recognize those two noncomforming lines of code as being something I had
previously used and was simply checking to see if the code in question was
somehow workable, albeit non-standard.

BTW, your version for the initialization of p was incorrect. Since
struct theline {
int x1,y1; // starting point
int x2,y2; // starting point
char color; // color
};
is the same as:
struct theline {
int x1;
int y1; // starting point
int x2;
int y2; // starting point
char color; // color
};
the correct initialization should be different than your version:
struct thepath p = {{{5, 6}, {10, 12}, 255},
{{5, 6}, {10, 12}, 255},
1};
and instead be:
path p = {{5, 6, 10, 12, 255},
{5, 6, 10, 12, 255},
1};

Later,
Gary Schnabl
(Southwest) Detroit - 2 miles NORTH! of Canada - Windsor, that is...
 
C

CBFalconer

Gary said:
.... snip ...

Since I noticed other errors in the tutorial, I took it upon
myself tonight to check some more of Prof. Valvano's code, and
that code example just happened to be the first one I decided to
check. (I didn't start at the beginning of the tutorial and
instead arbitrarily chose Chapter 9.) I didn't recognize those
two noncomforming lines of code as being something I had
previously used and was simply checking to see if the code in
question was somehow workable, albeit non-standard.

So why all the heat and defensiveness? You were shown, in detail,
where the code was just plain wrong. If this alleged Professor
actually is publishing these things he is shown to be completely
worthless. Maybe you should complain to his University. Maybe his
students should get refunds. At most you should make them aware of
his foolishness. Move on.
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top