Compiling error: expected identifier or ‘(’ before ‘{’ token

Discussion in 'C Programming' started by aydinh, Mar 10, 2010.

  1. aydinh

    aydinh Guest

    Hi I've been writing a piece of code as part of university course and
    I am getting an error and I can't figure out how to fix it.

    The code is meant to be a reverse polish notation calculator, the user
    inputs digits and operators one at a time and the program either
    pushes the numbers on to a stack or pops them off when it receives an
    operator, performs the calculation, then pushes the result back on to
    the stack.

    Finally when the user inputs a equals sign the program displays the
    results, this is how it's meant to work anyway.

    When I try to compile i get this error:
    prog5.c:10: error: expected identifier or ‘(’ before ‘{’ token

    BTW the simpleio.h is a library of input mechanisms given to us by our
    university, they include the getInt() & getChar functions, supposedly
    there to make our lives easier, anyway, the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include "../simpleio.h"

    typedef struct CalcParam { int item;
    struct calcParam * next;
    } calcParam;

    int main();
    {
    char item;
    int temp1, temp2, temp3, answer;
    calcParam * top;
    calcParam * cpp;

    printf("Enter a reverse polish notation string, character by
    character: \n");
    printf("Finish with '='\n");
    printf("Next: "); item=getChar();

    while(item!='=')
    {
    if (!(item>='0' && item<='9' || item=='+' || item=='-' || item=='*'
    || item=='/' ) //ccheck if input is valid
    {
    printf("Not a valid input, valid input: digits 0-9, +, -, *, /");
    exit(1)
    }

    if (item>='0' && item<='9') //check if user input is a
    number
    {
    if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); //check if
    memory can be allocated
    {
    printf("Cannot allocate memory\n");
    exit(1);
    }
    atoi(&item); //convert input char to int ready to push on
    to stack
    cpp->item=item; //push number on to stack
    cpp->next = top;
    top = cpp;
    printf("Next: "); item=getChar();
    }
    if (item=='+' || item=='-' || item=='*' || item=='/') //
    check if user input is an operator
    {
    temp1=top->item; //pop first item and put it in
    temp1
    cpp = top;
    top = top->next;
    free(ccp);
    temp2=top->item; //pop second item and put it in
    temp2
    cpp = top;
    top = top->next;
    free(ccp);
    temp3 = temp2 item temp1; //perform arithmetic and
    store answer in temp3

    if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); //
    check if memory can be allocated
    {
    printf("Cannot allocate memory\n");
    exit(1);
    }
    cpp->item=temp3; //push temp3 on to stack
    cpp->next = top;
    top = cpp;
    printf("Next: "); item=getChar();
    }
    }
    answer=top->item; //pop answer
    cpp = top;
    top = top->next;
    free(ccp);
    printf("Answer: ", answer);
    return 0;
    }
     
    aydinh, Mar 10, 2010
    #1
    1. Advertisements

  2. aydinh

    Tom St Denis Guest

    Here's your problem.

    Tom
     
    Tom St Denis, Mar 10, 2010
    #2
    1. Advertisements

  3. it would be nice if you indicated which was line 10. I know we can
    count but the posting process can mess things up. And suppose it was
    big number line 199, do I have to count that many lines to help you?
    I think you meant "struct CalcParam". Is this your actual code?
    I can find your error by inspection but you should learn to do this
    yourself. Which line was the error reported on? What came just before
    the { ? Ignore spaces, tabs and newlines. Now go and look in your
    textbook or course notes. How is a function defined?


    <snip>
     
    Nick Keighley, Mar 10, 2010
    #3
  4. I dealt witht he error in another post

    variables called temp1, temp2 and temp3 are usually a bad sign. Have
    you done arrays yet?
    did you use up your allocation of spaces? I'd code this as

    while (item != '=')
    have you be told about isdigit()? I'd worry about the precedence of &&
    and ||. I * think* the above is correct but I'd check.

    technically 1 (one) is a not a portable value to pass to exit().
    Though it's going to work most places.
    // comments don't work on older compilers and get messed up by posting
    s/w (as here).

    is malloc() really necessary? Your stack is a list of calcParams? You
    chekced the return value of malloc()- good! The cast is unnecessary in
    C.
    you ignore the return value of atoi(). atoi() expects a string you
    can't give it the address of a char. Review your pointer stuff. atoi()
    has no error checking but I suppose that's ok for a learner.
     
    I've removed some of your comments as the layout had gone *really*
    strange
    where was top initialised (I may have missed it). Have you done
    functions yet? push and pop probably should be functions.
         
    I hate comments like this! We *know* you are storing it in temp2.
     
    Nick Keighley, Mar 10, 2010
    #4
  5. aydinh

    aydinh Guest

    line 10 is the line after int main, which as pointed out above I
    ridiculously left in a semi-colon without noticing :(, the definition
    of struct calcParam is right, and yes it is my own code.
     
    aydinh, Mar 10, 2010
    #5
  6. aydinh

    aydinh Guest

    Thanks for all the comments, the code works now and I am going to read
    through all your suggestions, I have learnt functions and arrays, but
    this exercise is under the lab worksheet for malloc, so I guess it's
    best I use malloc.

    I do understand the precedence of || and &&, but haven't come across
    isdigit, will look in to it.

    exit(1) is just what I have been shown by my lecturer, I know no other
    way :(

    I did forget to initialise top, I have amended it and it now looks
    like this: calcParam * top = NULL;

    Sorry about the annoying comments :(
     
    aydinh, Mar 10, 2010
    #6
  7. it happens! But the compiler tries its best to help!
    must be me. I'd expect

    typedef struct CalcParam { int item;
    struct CalcParam * next;
    } calcParam;

    I'm surprised it can "see" calcParam when next is defined.
     
    Nick Keighley, Mar 10, 2010
    #7
  8. aydinh

    aydinh Guest

    I'm surprised it can "see" calcParam when next is defined.

    I know what you mean, but this is how I've been shown to do it, and
    both my reference books say to do it this way.
     
    aydinh, Mar 10, 2010
    #8
  9. Compilation fails here, as your compiler as told you.
    Don't use tabs. Your lines are too long even without them. My newsreader
    will complain when I try to post this because I've quoted /your/ lines.
    Ergo, you need a smarter newsreader. Try slrn.
    Character by character? Surely you can do better than that.
    What if item is EOF?
    Please stop using // comments. I can't even count all the syntax
    errors you've caused by them wrapping around all over the place.
    Compilation will fail here. Plus 1 is not a valid return value on
    all systems. stdlib.h has a constant EXIT_FAILURE you can use for
    that purpose.
    See ctype.h and isdigit().
    This is a terrible use of malloc(). Try:

    cpp = malloc(sizeof *cpp)
    What do you expect the to do? And how?
    What do you expect this to do? And how?
    Why did you duplicate this line?
    This is pointless. If you're going to free anything why don't
    you free the entire list?
     
    Andrew Poelstra, Mar 10, 2010
    #9
  10. Throw them both out. They're garbage.
     
    Andrew Poelstra, Mar 10, 2010
    #10
  11. Actually, if you count each tab as one character you are under 72 chars
    for each line. It was merely an illusion that you had more. My bad.
     
    Andrew Poelstra, Mar 10, 2010
    #11
  12. aydinh

    aydinh Guest

    I'm trying I only started programming 6 months ago for roughly 3 hours
    a week :(
     
    aydinh, Mar 10, 2010
    #12
  13. I was at first, but I think the error is a subtle one.

    Lets re-write without relying on case as the sole difference between
    the names:

    typedef struct stag { int i; struct tdef *next; } tdef;

    The inner struct tdef defines a pointer to a new (incomplete) struct
    type with a struct tag that just happens to be the same as the typedef
    name that is being defined.

    The OP should get an error when attempting to do anything with the
    next pointer because it refers to an incomplete type. That is indeed
    what happens, but to get that far I had to correct a whole host of
    other syntax errors. I think the OP is just assuming it's correct
    because there was not error at the point of definition.

    To the OP: your definition of the struct is not correct and the
    compiler will tell about it eventually.

    <snip>
     
    Ben Bacarisse, Mar 10, 2010
    #13
  14. In that case, I politely request that when posting code to Usenet,

    1. Don't use the tab key on your keyboard. The most consistent
    way that they display (assuming no servers on the chain delete
    them) is IMHO an ugly one.
    2. Don't use // comments. They wrap around and then your code
    can't be copy-pasted and still compile.

    Other than that you were netiquettically in the clear.
     
    Andrew Poelstra, Mar 10, 2010
    #14
  15. The point is that "calcParam" and "CalcParam" are two different
    identifiers. If you spell them consistently:

    typedef struct calcParam { int item;
    struct calcParam * next;
    } calcParam;

    then you're ok.
     
    Keith Thompson, Mar 10, 2010
    #15
  16. The problem isn't the tab key on the keyboard, it's the tab characters
    in the source. (In my editor with my configuration, pressing
    <tab> inserts spaces.)

    But yes, it's a good idea to avoid tab characters in code posted here.
    If you're on a Unix-like system, filtering the source through "expand"
    (or perhaps "expand -t 4") is a good solution.
     
    Keith Thompson, Mar 10, 2010
    #16
  17. I read his code a couple of times and it looked to me like he
    was okay:

    typedef struct _test {
    int data1;
    int data2;
    struct _test *next;
    } Test;

    I added an underscore for clarity, but that was essentially
    his definition, wasn't it? (I've lost the original message.)
     
    Andrew Poelstra, Mar 11, 2010
    #17
  18. No, he used two different names for the tag (and re-used one of those
    names for the typedef).

    Here's the declaration from the original post:

    typedef struct CalcParam { int item;
    struct calcParam * next;
    } calcParam;

    This is legal, but it creates a type "struct CalcParam" one of
    whose members is a pointer to an incomplete type "struct calcParam".

    (Note that using the same identifier for the struct tag and the
    typedef is perfectly ok. Or you can use a different name if you
    prefer, but you should pick some consistent convention.)
     
    Keith Thompson, Mar 11, 2010
    #18
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.