"typedef struct" is not pointer?

C

cerr

Why does this little sample app not compile? :eek:
I'm using the CCS compiler version 4.119 and i'm getting the error "A
numeric expression must appear here" by the function call where i
wanna pass Foo as an argument by reference.


Code:
#include <18F87K22.h>
#device HIGH_INTS=TRUE, adc=16, ICD=TRUE

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128
Postscale
#FUSES HSM //Hi-Speed crystal oscillator
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPLLEN //No PLL enabled
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed
Addressing mode disabled (Legacy mode)

typedef struct {
int16 var1;
int16 var2;
int16 var3;
}MyStruct;

MyStruct TestMe;

MyFunc(MyStruct *Foo);


void main (void)
{
TestMe.var1=1;
TestMe.var2=2;
TestMe.var3=3;
MyFunc(TestMe);
}

MyFunc(MyStruct *Foo)
{
Foo->var1=Foo->var2+Foo->var3;
}

Any hints?
Thanks,
Ron
 
K

Keith Thompson

cerr said:
Why does this little sample app not compile? :eek:
I'm using the CCS compiler version 4.119 and i'm getting the error "A
numeric expression must appear here" by the function call where i
wanna pass Foo as an argument by reference.

I'll note that it's unlikely anyone else will be able to compile this.
Code:
#include <18F87K22.h>

Non-standard header.
#device HIGH_INTS=TRUE, adc=16, ICD=TRUE

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128
Postscale
#FUSES HSM //Hi-Speed crystal oscillator
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPLLEN //No PLL enabled
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed
Addressing mode disabled (Legacy mode)

These are all non-standard directives. Note also that two of your lines
were wrapped. Either limiting your line lengths or using "/* ... */"
comments would avoid this problem.
typedef struct {
int16 var1;
int16 var2;
int16 var3;
}MyStruct;

What is int16?
MyStruct TestMe;

MyFunc(MyStruct *Foo);

Suggestion: declare the return type explicitly:

int MyFunc(MyStruct *Foo);

Or, since it doesn't actually return a result:

void MyFunc(MyStruct *Foo);

Implicit int was dropped in C99, and isn't very good style even in C90.

Hmm. "//" comments were introduced in C99, the same standard that
dropped implicit int. But they're a common extension in pre-C99
compilers.
void main (void)

I presume you're using a freestanding implementation (otherwise
"int main(void)" would be the correct declaration).
{
TestMe.var1=1;
TestMe.var2=2;
TestMe.var3=3;
MyFunc(TestMe);

Myfunc expects a pointer to a MyStruct, but you're passing it
a MyStruct. I don't know why this would cause the error message
you describe.
}

MyFunc(MyStruct *Foo)
{
Foo->var1=Foo->var2+Foo->var3;

Whitespace is your friend:

Foo->var1 = Foo->var2 + Foo->var3;

is much easier to read. This looks ok *if* that int16 is a numeric
type. If not, I'd expect an error message similar to "A numeric
expression must appear here". Hmm.

If MyFunc() actually returns an int result, you need a return statement
here. If not, again, you should declare it returning void.

A minor style point: personally, I probably wouldn't use the typedef.
Instead, I'd write:

struct MyStruct {
/* ... */
};

and then refer to "struct MyStruct". But if you want to have a one-word
name for the type, a typedef is the way to do it.
 

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

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top