开发者

error: expected ')' before '*' token

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 (开发者_JAVA技巧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?


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.


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.


In your code you have defined like this for memory.h

#ifndef MEMORY_H
#define MEMORY_H
...
...
#endif

In case any of your other files which you use in your project is having the same #define i.e MEMORY_H then you can get this error.

Solution:

#ifndef XYZ_MEMORY_H
#define XYZ_MEMORY_H
...
...
#endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜