expected ')' before '*' token, can't seem to find error - c

So whenever I try to run my Makefile on my server, it always gives me the error is "Memory.c: 9 error: expected ')' before '*' token. But when I try to run it on my own computer, it works just fine. I've been trying to figure out what is wrong but can't seem to find it.
I've attached the 3 files that are used in this part of my program. Memory.c, Memory.h and ProcessInput.h.
This is Memory.c
/* Initializes memory */
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
void initializeMemory(memory** memArray, int memSize)
{
// Allocating space for memory array
*memArray = malloc(memSize * sizeof(memory));
if(*memArray == NULL)
{
fprintf(stderr, "Error allocating space for array of memory" );
exit(1); // exit(1) = Unsuccessful exit
}
// Initializing the contents within memory array
int i = 0;
for(i = 0; i < memSize; i ++)
{
((*memArray)[i]).occupied = false;
}
}
and this is Memory.h
// Definitions for Memory.c
#define bool int
#define true 1
#define false 0
#include "ProcessInput.h"
// Include guards to prevent redefinition of struct
#ifndef MEMORY_H
#define MEMORY_H
typedef struct memoryDetail
{
process process;
bool occupied;
} memory;
#endif
// Function declaration for memory.c
void initializeMemory(memory** memArray, int memSize);
the only thing used from ProcessInput.h is the process structure defined in ProcessInput.h
This is ProcessInput.h
// Include guards to prevent redefinition of struct
#ifndef PROCESSDETAIL_H
#define PROCESSDETAIL_H
typedef struct processDetail
{
int timeCreated;
int processID;
int memorySize;
int jobTime;
} process;
#endif
// function declarations for ProcessInput.c
void processInput(int* maxSize, int* count, process** processes, char* fileName);
I'm not too sure why it's giving me the error. I don't know where I'm supposed to be putting a missing right brace. Any advice is much appreciated!
edit: As informed, these are the following questions that I looked at but to not avail.
error: expected ‘)’ before ‘*’ token
Multiple of same error while compiling "error: expected ')' before '*' token
http://www.dreamincode.net/forums/topic/288956-error-expected-before-token/
thanks everyone for the help!

#include "memory.h" is different to #include "Memory.h" (i.e. C is case sensitive)
If you tried #include "myfile.h" instead of #include "MyFile.h" the error may be more obvious. In this case it just happens that the compiler finds the system memory.h.

<memory.h> is a header from C library of pre-standard era. It is quite possible that your standard library still provides it and the compiler takes that one instead of yours.
Try renaming your header file and see if it changes anything.

Related

Undefined symbol even when include header file?

When I try to compile my code, I'm getting the error:
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
I read that "Undefined symbol errors can occur when a function is declared (as is the case for the lock functions in lock.h), but it is not properly implemented." But I feel that I am implementing all the functions in lock.h properly since all the functions are of type voidand I don't return anything. So I don't know what I could be doing wrong to get this error.
Any help would greatly be appreciated!
Note: Please let me know if I need to provide more code. I don't think I need to write what I put in the function implementations of lock.h since I feel all you need to know is that the function implementations do not return anything, but please let me know if I need to include that.
Code
lock.c
#include "lock.h"
extern process_t * current_process;
extern process_t * process_queue;
void l_init(lock_t* l){
//Does stuff but never type return or return anything
}
void l_lock(lock_t* l){
//Does stuff but never type return or return anything
}
void l_unlock(lock_t* l){
//Does stuff but never type return or return anything
}
lock.h
#ifndef __LOCK_H_INCLUDED__
#define __LOCK_H_INCLUDED__
#include "3140_concur.h"
#include "shared_structs.h"
void l_init(lock_t* l);
void l_lock(lock_t* l);
void l_unlock(lock_t* l);
#endif /* __LOCK_H_INCLUDED */
3140_concur.c
#include "3140_concur.h"
#include <stdlib.h>
3140_concur.h
#ifndef __3140_CONCUR_H__
#define __3140_CONCUR_H__
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "process.h"
void process_blocked (void);
void process_terminated (void);
unsigned int * process_stack_init (void (*f)(void), int n);
void process_stack_free (unsigned int *sp, int n);
void process_begin (void);
#endif
process.h
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "3140_concur.h"
struct process_state;
typedef struct process_state process_t;
unsigned int * process_select (unsigned int * cursp);
extern process_t * current_process;
extern process_t * process_queue;
void process_start (void);
int process_create (void (*f)(void), int n);
process.c
(I do process_t* process_queue = NULL; & process_t* current_process = NULL; b/c I want them to be NULL before any functions are called)
#include "3140_concur.h"
#include "shared_structs.h"
#include <stdlib.h>
#include <fsl_device_registers.h>
process_t* process_queue = NULL;
process_t* current_process = NULL;
lab4_t0.c
#include "process.h"
#include "utils.h"
#include "lock.h"
lock_t l;
\\uses functions in lock.h
Update
Build output
*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling 3140_concur.c...
compiling lab4_t0.c...
lab4_t0.c(42): warning: #111-D: statement is unreachable
return 0;
lab4_t0.c: 1 warning, 0 errors
compiling process.c...
process.c(100): warning: #1-D: last line of file ends without a newline
}
process.c: 1 warning, 0 errors
linking...
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
Not enough information to list image symbols.
Finished: 1 information, 0 warning and 3 error messages.
".\Objects\Lab4.axf" - 3 Error(s), 2 Warning(s).
Target not created.
Build Time Elapsed: 00:00:39
Thanks to #AjayBrahmakshatriya, it turns out I didn't add lock.c to my project. That fixed everything. Whew.

Defining a struct within a separate executable evidence file

I keep trying to compile my separate executable file for all of my tests, and get this error:
"variable has incomplete type 'struct observations', 'struct preferences', 'struct climate'"?
#include <stdio.h>
#include "file4.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
void eanalyze_weather()
{
struct observations day1;
day1.temp = 70.0;
day1.humid = 30.0;
day1.wind = 5.0;
struct observations day2;
day2.temp = 74.0;
day2.humid = 24.0;
day2.wind = 0.0;
struct observations day3;
day3.temp = 84.0;
day3.humid = 18.0;
day3.wind = 10.0;
int main(void)
{
econcat();
euint_to_binary();
ebinary_to_uint();
eanalyze_weather();
return 0;
}
When I try to run everything in "main" in a single file, it compiles properly and the code works, but when I try to do it this way, it is not compiling. Does anyone know why this is?
Thank you.
This error is because the definition of struct observations is not visible from the file containing main(), since the definition in present in some other file [separate translation unit].
One workaround (and the usual best practice) is to put the definition of the structure in the header file and don't forget to include some include guard.

error: expected identifier or '(' before 'typedef'|

i'm working since a few weeks on the same project and never had this kind or error.
I got it now without touching to the concerned file, which is down there:
#ifndef DIJKSTRA_H_INCLUDED
#define DIJKSTRA_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINI 100000.0
typedef struct dij
{
int ordre;
float** adjacencePoids;
float* l;
int* pred;
}t_dij;
int choix_action();
int choix_sommet_depart();
int choix_sommet_arrivee();
t_dij* allouer_dijkstra();
t_dij* allouer_dijktra_durees();
t_dij* dijkstra();
void afficher_resultat();
void sauver_resultat();
void detruire_struc();
#endif // DIJKSTRA_H_INCLUDED
This code gives me this error (Compiler is MinGW)
Line 11 error: expected identifier or '(' before 'typedef'|
Thanks!
I suspect the error lies in the file the header shown is included from, or in a file included just before this one.

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 (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

Clear tutorial explaining modular programming in C?

I'm just getting started with modular programming in C. I think I'm doing something wrong with the inclusions, because I'm getting a lot of conflicting types for 'functionName' and previous declaration of 'functionName' was here errors. I did put inclusion guards in place.
Do you know a clear tutorial that explains modular programming in C, especially how the inclusions work?
Update: I have tried to isolate my issue. Here's some code, as requested.
Update 2: updated code is below. The errors have been updated, too.
/*
* main.c
*/
#include <stdio.h>
#include "aStruct.h"
int main() {
aStruct asTest = createStruct();
return 0;
}
/*
* aStruct.h
*/
#ifndef ASTRUCT_H_
#define ASTRUCT_H_
struct aStruct {
int value1;
int value2;
struct smallerStruct ssTest;
};
typedef struct aStruct aStruct;
aStruct createStruct();
#endif /* ASTRUCT_H_ */
/*
* smallerStruct.h
*/
#ifndef SMALLERSTRUCT_H_
#define SMALLERSTRUCT_H_
struct smallerStruct {
int value3;
};
typedef struct smallerStruct smallerStruct;
smallerStruct createSmallerStruct();
#endif /* SMALLERSTRUCT_H_ */
/*
* aStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
#include "aStruct.h"
aStruct createStruct() {
aStruct asOutput;
printf("This makes sure that this code depends on stdio.h, just to make sure I know where the inclusion directive should go (main.c or aStruct.c).\n");
asOutput.value1 = 5;
asOutput.value2 = 5;
asOutput.ssTest = createSmallerStruct();
return asOutput;
}
/*
* smallerStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
smallerStruct createSmallerStruct() {
smallerStruct ssOutput;
ssOutput.value3 = 41;
return ssOutput;
}
This generates the following error messages:
At aStruct.h:10
field 'ssTest' has incomplete type
At main.c:8
unused variable `asTest' (this one makes sense)
The base of inclusion is to make sure that your headers are included only once. This is usually performed with a sequence like this one:
/* header.h */
#ifndef header_h_
#define header_h_
/* Your code here ... */
#endif /* header_h_ */
The second point is to take care of possible name conflicts by handling manually pseudo namespaces with prefixes.
Then put in your headers only function declarations of public API. This may imply to add typedefs and enums. Avoid as much as possible to include constant and variable declarations: prefer accessor functions.
Another rule is to never include .c files, only .h. This is the very point of modularity: a given module dependant of another module needs only to know its interface, not its implementation.
A for your specific problem, aStruct.h uses struct smallerStruct but knows nothing about it, in particular its size for being able to allocate an aStruct variable. aStruct.h needs to include smallerStruct.h. Including smallerStruct.h before aStruct.h in main.c doesn't solve the issue when compiling aStruct.c.
The multiple definition problem is most likely coming from the way you're including the code. You are using #include "aStruct.c" as opposed to #include "aStruct.h". I suspect you are also compiling the .c files into your project in addition to the #include. This causes the compiler to become confused due to the multiple definitions of the same function.
If you change the #include to #include "aStruct.h" and make sure the three source files are compiled and linked together, the error should go away.
Such errors mean that function declaration (return type or parameter count/types) differs from other function declarations or function definition.
previous declaration message points you to the conflicting declaration.

Resources