"typedef struct" is not pointer?

Discussion in 'C Programming' started by cerr, Mar 31, 2011.

  1. cerr

    cerr Guest

    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
     
    cerr, Mar 31, 2011
    #1
    1. Advertising

  2. cerr <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 31, 2011
    #2
    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. Replies:
    2
    Views:
    510
  2. S Austin

    const in typedef struct?

    S Austin, Jan 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    370
    donLouis
    Jan 2, 2004
  3. Beefheart

    typedef struct _iobuf FILE;

    Beefheart, Apr 30, 2004, in forum: C Programming
    Replies:
    9
    Views:
    7,834
    those who know me have no need of my name
    May 4, 2004
  4. Immo Birnbaum

    Pointer/typedef struct questions

    Immo Birnbaum, Mar 20, 2005, in forum: C Programming
    Replies:
    2
    Views:
    1,798
    Barry Schwarz
    Mar 21, 2005
  5. Steven T. Hatton
    Replies:
    2
    Views:
    682
Loading...

Share This Page