9

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?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

3 Answers3

17

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.
llasram
  • 4,417
  • 28
  • 28
  • To avoid future problems, is there a quick reference for standard C header names? – Federico klez Culloca Sep 15 '10 at 17:25
  • @klez That appears to be an existing question: http://stackoverflow.com/questions/2027991/list-of-standard-header-files-in-c-and-c . In this case though, the standards-related lists wouldn't have helped, because `memory.h` isn't a standard header. You can always see what your particular OS provides with something like `find /usr/include/ -type f -name '*.h'`. – llasram Sep 15 '10 at 17:37
  • 3
    Provided, of course, that your particular OS has `find`, and keeps headers in `/usr/include/`. Not all people are lucky enough to run such OSes. – David Thornley Sep 15 '10 at 17:41
3

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.

Student
  • 1,197
  • 4
  • 22
  • 39
-1

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
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104