Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - expected expression before '=' token... on line without '='

I'm going crazy trying to figure out this error message that has no obvious connection to reality/my code. I've been searching on here and come to one conclusion: you're going to hate the pointer hidden by typedef. Sorry, it's out of my control--prof provided the code that way. I'm editing the code as specified in the problem. I'm popping full nodes to avoid malloc calls on each push function and storing them in a secondary stack. The MakeEmptyS function initializes a Stack with INITIAL_SIZE nodes. GrowEmptyS adds more nodes to the Stack of empty nodes

stack.c has the following function:

void
MakeEmptyS( Stack S )
{
  PtrToNode tmp;
  if ( S == NULL )
    Error( "Must use CreateStack first" );
  else
  {
    GrowEmptyS( S, INITIAL_SIZE);
    while (!IsEmptyS( S) )
    {
        tmp = TopopNode( S );
        PushEmpty( S, tmp);
    }
  }
}

I get this error: "Stack.c:53:22: error: expected expression before '=' token", where line 53 is GrowEmptyS( S, INITIAL_SIZE );

For reference, here is the Grow function:

   void
   GrowEmptyS( Stack S, int NumToAdd )
   {
       int i;
       PtrToNode TmpCell;
       for( i = 0; i < NumToAdd; i++ )
       {
         TmpCell = malloc( sizeof(struct Node));
         if ( TmpCell == NULL )
           FatalError( "Out of Space!!!");
         else
           PushEmpty(S,TmpCell);
       }
   }
like image 232
TravisThomas Avatar asked Oct 30 '11 01:10

TravisThomas


People also ask

What is expected expression before token in C?

A1: The error means that the compiler didn't expect you to assign an array to a scalar. When you specify rob_leftcolor[3] = {1.0, 0.0, 0.0}; , you tell compiler, I want you to assign vector of values {1.0, 0.0, 0.0} to 4th element of array rob_leftcolor (counting starts from 0 - 0, 1, 2, 3).

What does expected primary expression before token mean?

expected primary-expression before ')' token. Sometimes this happens when the variable passed into a function isn't the type the function expected. Make sure variables are defined in the correct scope and that the types match the definition.

What does Expected expression mean in C?

Expected expression. This error is produced whenever the compiler is expecting an expression on the line where the error occurred. In the following example, the trailing comma in the initializer indicates to the compiler that another expression will follow.


1 Answers

I may be wrong but probably you defined

#define INITIAL_SIZE = 1024

for example.

You should remove the =.

The correct definition would be

#define INITIAL_SIZE 1024

As an advice, function parameters should start with lower case, not upper case :)

void GrowEmptyS(Stack stack, int numToAdd)

is easier to read.

like image 181
Salvatore Previti Avatar answered Oct 24 '22 06:10

Salvatore Previti