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];
};
Related
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.
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.
i'm receiving the next error:
expected specifier-qualifier-list before 'typedef' (in challenge_room_system_fields.h):
This is challenge_system.h:
typedef struct SChallengeRoomSystem
{
#include "challenge_room_system_fields.h"
} ChallengeRoomSystem;
This is challenge_room_system_fields.h:
#include "challenge_system.h"
typedef struct SChallengeRoomSystem //this is where i get the error
{
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
Can someone help me figure out what's wrong?
I know its not the best way to deal with structs but as a school assignment i'm required not to change the challenge_system.h. I am allowed to change only challenge_room_system_fields.h
Thanks in advance....
i should've just write the following inside challenge_room_system_fields.h :
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
The reason is that the #include by definition takes the included file and Copy-Pastes it instead of the line I wrote #include...
so eventually challenge_system.h will look as follows:
typedef struct SChallengeRoomSystem
{
// this is where previously i had: #include "challenge_room_system_fields.h"
//now i will have the following:
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
And now it works:)
Structs containing arrays in C51 are not allowed? After defining a simple structure in my C51 program,
struct RingBuffer
{
int zero;
int size;
int capacity;
char data[10];
};
I got error:..\SOURCE\MYRINGBUFFER.H(25): error C141: syntax error near '['. It is clear that error is on the line with a char array defined in the struct(no errors after commenting).
Am I doing something wrong here? If not, is there anyway I can achieve what I'm attempting to do?
EDIT: All the code.
#ifndef __MY_RING_BUFFER_H__
#define __MY_RING_BUFFER_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct RingBuffer
{
int zero;
int size;
int capacity;
char data[10];
};
#endif
I figured out what's wrong. data is a keyword in C51.
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.