Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: expected ')' before '*' token

Tags:

c

I have this include file (memory .h)

#ifndef MEMORY_H
#define MEMORY_H

#ifdef  __cplusplus
extern "C" {
#endif

    typedef struct mmemory {
        int* cells;
        int* current_cell;
        int cells_number;
    } memory;

    void memory_init(memory* mymemory, int size);
    void step_left(memory* mymemory, int steps);
    void step_right(memory* mymemory, int steps);
    void cell_inc(memory* mymemory, int quantity);
    void print_cell(memory* mymemory);
    void get_char(memory* mymemory);


#ifdef  __cplusplus
}
#endif

#endif  /* MEMORY_H */

And this implementation file (memory.c)

#include <stdlib.h>
#include "memory.h"

void
memory_init (memory* mymemory, int size)
{
    mymemory->cells = (int*) malloc (sizeof (int) * size);
    mymemory->cells_number = size;
    mymemory->current_cell = (int*) ((mymemory->cells_number / 2) * sizeof (int));
}
... //other function definitions follow

When I try to compile memory.c I get this error for each and every function definition

src/memory.c:5: error: expected ')' before '*' token

where line 5 is the function definition for memory_init()

Can someone please tell me why I'm getting this error?

like image 694
Federico klez Culloca Avatar asked Sep 15 '10 17:09

Federico klez Culloca


People also ask

How do you fix error expected before token?

To fix this error, check the statement which should not be terminated and remove semicolons. To fix this error in this program, remove semicolon after the #define statement.

How do you fix expected unqualified ID before token?

– Adjust the Curly Braces To Fix the Expected Unqualified Id Error. You should match the opening and closing curly braces in your code to ensure the right quantity of brackets. The code should not have an extra or a missing curly bracket.

What does error expected before token mean?

Typically this means that the library that contains 'cout' isn't present in your code. You can read about 'cout' declarations here: http://www.cplusplus.com/reference/iostream/cout/ expected ';' before '}' token. This typically means that there is statement missing a semicolon ';'.

What is the meaning of error expected?

expected” This error occurs when something is missing from the code. Often this is created by a missing semicolon or closing parenthesis.


2 Answers

Because the system memory.h is shadowing your memory.h, causing the #include to succeed without declaring your types. Several possible fixes:

  • Rename your file -- probably for the best in any case, to reduce potential confusion.
  • Include your file via a prefix subdirectory (e.g., #include <myproj/memory.h>).
  • Move your file into the same directory as the source file, allowing the #include precedence rules for filenames wrapped in " to take effect.
  • Ensure that your C pre-processor include path options place your project header path prior to the system header paths.
like image 91
llasram Avatar answered Oct 19 '22 17:10

llasram


This answer is really late, but I encountered a similar problem.

I think your problem is related to a typo in your .h file where you declare a struct mmemory. If you remove that extra 'm' it should work.

like image 29
Tom Groentjes Avatar answered Oct 19 '22 18:10

Tom Groentjes