Header file error: expected identifier or '(' before numeric constant - c

I'm new to header files and am not sure why I am getting this error. The first piece of code is from the relevant header file, and gives the expected identifier error:
#define MAX_ADDR_LENGTH 1000
struct listNode{
char addr[MAX_ADDR_LENGTH];
struct listNode *next;
};
Related to this, there is another error in the file relevant to that header, which gives me a "note: in expansion of macro 'MAX_ADDR_LENGTH', which it gives me on the line which gives the declaration of int MAX_ADDR_LENGTH:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
char *crawl(char* getLinksFrom, int hopTo){
int MAX_ADDR_LENGTH = 300;
char startAddr[MAX_ADDR_LENGTH];
char destAddr[MAX_ADDR_LENGTH];
}
I've tried a number of things hoping it was just a small oversight (removed the #define altogther, deleted the line that gives the int MAX_ADDR_LENGTH declaration, just deleted the phrase 'int' from the same; all of which just caused even more errors).

The problem: ‘MAX_ADDR_LENGTH’ is defined twice in your code; Once as a Macro and once as a variable.
Try to delete the statement declaring MAX_ADDR_LENGTH as a variable.

Build your header like this:
#ifndef HEADER_H
#define HEADER_H
#define MAX_ADDR_LENGTH 1000
typedef struct _listNode{
char addr[MAX_ADDR_LENGTH];
struct _listNode *next;
} listNode;
#endif /* HEADER_H */
and use it like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
char *crawl(char* getLinksFrom, int hopTo){
char startAddr[MAX_ADDR_LENGTH];
char destAddr[MAX_ADDR_LENGTH];
.... more code
}
The #ifndef...#endif construct is called an include guard. It's not necessary to get your code compiled, but good practice to use it.
The typedef is used to define the node structure. It does not create a variable yet, it's just defining a type named listNode that you can use to build your list later on.

Related

How can I fix an forward declaration warning in a structure

I have several structures, and I keep getting a warning message. I've been trying for several hours to kick it out, but I can't.
I would really appreciate the help.
This is the Student struct in the Student.h header:
#ifndef __STUDENT_H__
#define __STUDENT_H__
#include "Teacher.h"
#include "ClassRoom.h"
struct Teacher;
struct ClassRoom;
typedef struct {
char * name;
struct ClassRoom *myClassRoom;
struct Teacher *myTeachers[3];
} Student;
void setTeacherDynamic(struct Teacher *t, struct Teacher* tt);
and this is the Student.c source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Teacher.h"
#include "Student.h"
struct Teacher;
void setTeacherDynamic(Teacher *t, Teacher* tt)
{
strcpy(t->name, tt->name);
t->myClass = NULL;
}
The warning message is:
The problem is with the function setTeacherDynamic(), and I don't know how to fix it.
The Teacher struct:
and this is when I'm calling to the function, also in Student.c
You have multiple problems:
The first is that since you have forward declarations in Student.h you don't need to include Teacher.h or ClassRoom.h. And in the source file Student.c you can remove the forward declaration you have.
The second problem is that you don't actually have a struct Teacher. You have a Teacher type-alias for an anonymous structure. You should declare the actual structure for the forward declarations to work:
typedef struct Teacher { ... } Teacher;
You should do these changes for all your source and header files.

C false error: dereferencing pointer to incomplete type [duplicate]

This question already has answers here:
C programming decoupling interface from implementation with struct forward declaration
(2 answers)
Closed 6 years ago.
in my project i have three files: PlayBoard.c, Board. c, Board.h, who's relevant parts appear bellow. within PlayBoard i have theBoard which is a pointer to a variable of type struct Board.
when trying to compile i constantly get a "dereferencing pointer to incomplete type".
moving Board's definition to Board.h helps, but is not possible (it's a homework question), so i need another way.
PlayBoard.c:
#include "Board.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
...
int main()
{
BoardP theBoard =//creation of new Board;
...
}
Board.h:
#ifndef BOARD_H
#define BOARD_H
// ------------------------------ includes ------------------------------
#include <stdbool.h>
#include <stdio.h>
// -------------------------- definitions -------------------------
/**
* A pointer to Gomoku table.
*
*/
typedef struct Board* BoardP;
/**
* A struct of a Gomoku table.
*/
typedef const struct Board* ConstBoardP;
...
#endif
and Board.c:
#ifndef BOARD_C
#define BOARD_C
#include "Board.h"
typedef struct Board
{
char **_board;
int _cols;
int _rows;
bool _turn;
int _xCounter;
int _oCounter;
int _lastx;
int _lasty;
} Board;
...
#endif
The complete type of your struct is in Board.c, which your PlayBoard.c knows nothing about. All it has is a declaration of a struct that you have yet to define (in your Board.h file). Move the definition of the struct to within the header file. Barring this, ensure that PlayBoard.c only uses a pointer to Board (or a BoardP, as per your typedef). You do not need a complete type to declare a pointer. This ensures that any usage of the struct Board is confined to Board.c (which I assume is the point of your assignment).

Error: storage size of 'c' isn't known

I'm getting "Error: storage size of 'c' isn't known" when i try to create the variable "c" of my struct called "struct car"
Here is the code:
teste.h
#ifndef TESTE_H_INCLUDED
#define TESTE_H_INCLUDED
typedef struct car Car;
#endif // TESTE_H_INCLUDED
teste.c
#include <stdio.h>
#include <stdlib.h>
#include "teste.h"
struct car{
char name[20];
char model[20];
};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "teste.h"
int main()
{
Car c;
return 0;
}
I can't figure out why I'm getting this error... I bet it is something stupid... could someone help me?
the Car structure on the header file is just a forward declaration, when it's included on main.c, it only has knowledge of the forward declaration but no clue of how it's defined (or in this case, what the size of the struct is). define your struct on the header file.
struct car {
char name[20];
char model[20];
};

C Typedef - Incomplete Type

So, out of the blue, the compiler decides to spit this in face:
"field customer has incomplete type".
Here's the relevant snippets of code:
customer.c
#include <stdlib.h>
#include <string.h>
#include "customer.h"
struct CustomerStruct;
typedef struct CustomerStruct
{
char id[8];
char name[30];
char surname[30];
char address[100];
} Customer ;
/* Functions that deal with this struct here */
customer.h
A header file for customer.h
#include <stdlib.h>
#include <string.h>
#ifndef CUSTOMER_H
#define CUSTOMER_H
typedef struct CustomerStruct Customer;
/* Function prototypes here */
#endif
This is where my problem is:
customer_list.c
#include <stdlib.h>
#include <string.h>
#include "customer.h"
#include "customer_list.h"
#include "..\utils\utils.h"
struct CustomerNodeStruct;
typedef struct CustomerNodeStruct
{
Customer customer; /* Error Here*/
struct CustomerNodeStruct *next;
}CustomerNode;
struct CustomerListStruct;
typedef struct CustomerListStruct
{
CustomerNode *first;
CustomerNode *last;
}CustomerList;
/* Functions that deal with the CustomerList struct here */
This source file has a header file, customer_list.h ,but I don't think its relevant.
My Problem
In customer_list.c, at the line with the comment /* Error Here */, the compiler complains about field customer has incomplete type.
I've been googling this problem all day, and now im at the point of pulling out my eyeballs and blending them with strawberries.
What is the source of this error ?
Thanks in advance :)
[P.S. if I forgot to mention something, let me know. Its been a stressful day for me, as you might tell ]
Move the struct declaration to the header:
customer.h
typedef struct CustomerStruct
{
...
}
In C, the compiler needs to be able to figure out the size of any object that is referenced directly. The only way that the sizeof(CustomerNode) can be computed is for the definition of Customer to be available to the compiler when it is building customer_list.c.
The solution is to move the definition of the struct from customer.c to customer.h.
It seems that something like
typedef struct foo bar;
won't work without the definition in the header. But something like
typedef struct foo *baz;
will work, as long as you don't need to use baz->xxx in the header.
What you have is a forward declaration of Customer structure that you are trying to instantiate. This is not really allowed because compiler has no idea about the structure layout unless it sees it definition. So what you have to do is move your definition from the source file into a header.

New to C... struct can't find a variable I identified

I'm new to this whole C thing, but I keep getting this error with my code
UArray2.c:19:error: request for member ‘i’ in something not a structure or union
It's obviously the uarray.i in my main function, but I don't get why it isn't seeing it.
This is my .h file. Not too interesting...
//UArray2.h
#include <stdlib.h>
#include <stdio.h>
#ifndef UARRAY2_INCLUDED
#define UARRAY2_INCLUDED
#define T UArray2_T
typedef struct T *T;
#undef T
//#undef UARRAY2_INCLUDED //undef?
#endif
This is my .c file. Pretty simple stuff.
//UArray.c
#include <stdlib.h>
#include <stdio.h>
#include "UArray2.h"
#define T UArray2_T
struct T{
int i;
};
int main()
{
UArray2_T uarray;
uarray.i=0;
return 0;
}
#undef T
So, does anyone have any idea as to why I'm getting this compile error? It's likely something stupid that I did.
In the header file you have
typedef struct T *T;
This means that when you declare the variable uarray you are actually declaring a pointer. So you should initialize the i member as
uarray->i = 0;
This will however most likely crash, as the pointer is uninitialized and can point to any location in memory. Either allocate memory for the pointer
UArray2_T uarray = malloc(sizeof(*uarray));
Or make it point to another structure
struct UArray2_T real_uarray;
UArray2_T uarray = &real_uarray;
I think there is a problem with the initialization as you are using the pointer in the header file.
typedef struct T *T;
You are actually pointing to the memory location by declaring uarray.
Try to rectify this error.

Resources