C语言 错误:在 '*' 标记之前应为 ')'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3720034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: expected ')' before '*' token
提问by Federico klez Culloca
I have this include file (memory .h)
我有这个包含文件 ( 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)
而这个实现文件 ( 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.cI get this error for each and every function definition
当我尝试编译时memory.c,每个函数定义都会出现此错误
src/memory.c:5: error: expected ')' before '*' token
src/memory.c:5: 错误:在 '*' 标记之前预期为 ')'
where line 5 is the function definition for memory_init()
其中第 5 行是函数定义 memory_init()
Can someone please tell me why I'm getting this error?
有人可以告诉我为什么会收到此错误吗?
回答by llasram
Because the system memory.his shadowing your memory.h, causing the #includeto succeed without declaring your types. Several possible fixes:
因为系统memory.h正在隐藏您的memory.h,导致#include成功而无需声明您的类型。几个可能的修复:
- 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
#includeprecedence 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.
- 重命名您的文件——在任何情况下都可能是最好的,以减少潜在的混乱。
- 通过前缀子目录(例如,
#include <myproj/memory.h>)包含您的文件。 - 将您的文件移动到与源文件相同的目录中,从而使
#include包含在其中的文件名的优先规则"生效。 - 确保您的 C 预处理器包含路径选项将您的项目头文件路径置于系统头文件路径之前。
回答by Tom Groentjes
This answer is really late, but I encountered a similar problem.
这个回答真的晚了,但是我遇到了类似的问题。
I think your problem is related to a typo in your .hfile where you declare a struct mmemory. If you remove that extra 'm' it should work.
我认为您的问题与您声明 struct mmemory 的.h文件中的拼写错误有关。如果您删除那个额外的“m”,它应该可以工作。
回答by jignesh63
In your code you have defined like this for memory.h
在你的代码中,你已经为 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.
如果您在项目中使用的任何其他文件具有相同的 #define 即 MEMORY_H,那么您可能会收到此错误。
Solution:
解决方案:
#ifndef XYZ_MEMORY_H
#define XYZ_MEMORY_H
...
...
#endif

