I have read about 5 different questions on the same error, but I still can't find what's the problem with my code.
main.c
int main(int argc, char** argv) {
//graph_t * g = graph_create(128); //I commented this line out to make sure graph_create was not causing this.
graph_t * g;
g->cap; //This line gives that error.
return 1;
}
.c
struct graph {
int cap;
int size;
};
.h
typedef struct graph graph_t;
Thanks!
You can't do that since the struct is defined in a different source file. The whole point of the typedef is to hide the data from you. There are probably functions such as graph_cap and graph_size that you can call that will return the data for you.
If this is your code, you should define struct graph inside the header file so all files that that include this header will be able to have the definition of it.
When the compiler is compiling main.c it needs to be able to see the definition of struct graph so that it knows there exists a member named cap. You need to move the definition of the structure from the .c file to the .h file.
An alternate method, if you need graph_t to be an opaque data type, is to create accessor functions that take a graph_t pointer and return the field value. For example,
graph.h
int get_cap( graph_t *g );
graph.c
int get_cap( graph_t *g ) { return g->cap; }
Must be the order you have things defined. The typedef line needs to show up in the header file that is included by the file that has main().
Otherwise it worked fine for me.
lala.c
#include "lala.h"
int main(int argc, char** argv) {
//graph_t * g = graph_create(128); //I commented this line out to make sure graph_create was not causing this.
graph_t * g;
g->cap; //This line gives that error.
return 1;
}
lala.h
#ifndef LALA_H
#define LALA_H
struct graph {
int cap;
int size;
};
typedef struct graph graph_t;
#endif
This compiles without problems with:
gcc -Wall lala.c -o lala
Related
this error seems very easy to fix but i've been trying and have no clue.
So i have three files:
symtable.h:
typedef struct symbolTable *SymTable_T;
symtablelist.c:
#include "symtable.h"
struct Node{
char* key;
void* value;
struct Node* next;
};
struct symbolTable{
struct Node* head;
int length;
};
SymTable_T SymTable_new(void){
/* code */
}
And main.c:
#include "symtable.h"
int main(int argc, const char * argv[]) {
// insert code here...
SymTable_T emptyTable = SymTable_new();
emptyTable->length = 3; <------- ERROR
return 0;
}
I'm getting error: Incomplete definition of type "struct symbolTable"
Can anyone please give me a hint?
The reason i declare my struct in my source file is that i will have another implementation for the header file. so is there another way to fix my bug beside moving my struct declaration?
You can't access the members directly with an opaque pointer - if you keep the implementation in a separate source file, you'll have to access all the members via your interface, and not directly mess with the struct.
For instance, add this to symtable.h:
void SymTable_set_length(SymTable_T table, int len);
this to symtablelist.c:
void SymTable_set_length(SymTable_T table, int len)
{
table->length = len;
}
and in main.c change this:
emptyTable->length = 3;
to this:
SymTable_set_length(emptyTable, 3);
although in this specific case passing the length as an argument to SymTable_new() is an obviously superior solution. Even more superior is not letting the user set the length of a linked list data structure at all - the length is the number of items in it, and it is what it is. It would make no sense to, for instance, add three items to the list, and then allow main.c to set the length to 2. symtablelist.c can calculate and store the length privately, and main.c can find out what the length is, but it doesn't make much sense for main.c to be able to set the length directly. Indeed, the whole point of hiding the members of a struct behind an opaque pointer like this is precisely to prevent client code from being able to mess with the data like that and breaking the data structure's invariants in this manner.
If you want to access the members directly in main.c, then you have to have the struct definition visible, there is no alternative. This will mean either putting the struct definition in the header file (recommended) or duplicating it in main.c (highly unrecommended).
In typedef symbolTable *SymTable_T;, you refer to a non-existent type symbolTable. In C (unlike C++) the type is named struct symbolTable. (Note: the question has changed to fix this since answering it.)
There's a second problem. In main.c the code will need to be able to see the definition of struct symbolTable for you to be able to refer to fields of emptyTable. At the moment, the definition is hidden in a .c file... it should be moved to the header.
Can you explain why the struct Test is incomplete and how to remove the error? Is the error related to declaration in test.h or to definition in test.c? I tried to move the definition code to header file but then createTest does not know type Test or if I move the function to header there is the error multiple definition of createTest
test.h
typedef struct STest Test;
test.c
typedef struct STest {
int v;
char *str;
}Test;
Test *createTest(int v,char *str) {
Test *t=(Test*)malloc(sizeof(Test));
t->v=v; // error
t->str=str; // error
return t;
}
main function (main.c)
error:
main.c|44|error: dereferencing pointer to incomplete type
Put
typedef struct STest {
int v;
char *str;
} Test;
into test.h.
typedef struct STest Test only says that Test is another name for struct STest. At the moment, that's all that main.c knows. Especially, main.c doesn't know which members the struct has. That sounds quite incomplete to me.
If you don't define the structure in the header file it will not be visible from your main.c.
You need to do the following
Point 1. Put the structure definition in the test.h header file. use include guard also.
#ifndef __MY_HDR_H_
#define __MY_HDR_H_
typedef struct STest {
int v;
char *str;
}Test;
#endif //__MY_HDR_H_
[EDIT: Also, you need to add the function prototype for createTest() in the .h file]
Point 2. include test.h in test.c and main.c.
Point 3. Compile using
gcc main.c test.c -o output
Standard Warning : Please do not cast the return value of malloc() and family.
Place the code in the header file.
typedef struct STest {
int v;
char *str;
} Test;
Because compiler doesn't know to dereference that.
Hint: Then don't cast the result of malloc.
You seem to define Test twice.
In test.h you do
typedef struct STest Test;
Inside test.c remove the typedef and just do:
struct STest {
int v;
char *str;
};
A full example below:
To define an opaque type, a type which is know in detail only to the translation unit implementing its functions, you might take the following approach:
opaque.h
#ifndef OPAQUE_H
#define OPAQUE_H
typedef struct S T;
T * createT(int, char *);
#endif
opaque.c
include <stdlib.h>
#include "opaque.h"
struct S
{
int i;
char * p;
};
T * createT(int i, char * p)
{
T * t = malloc(sizeof *t);
if (NULL != t)
{
t->i = i;
t->p = p;
}
return t;
}
And use it like follows:
#include "opaque.h"
int main(void)
{
T * t = createT(0, NULL);
}
Well, I'm getting this error and couldn't identify when i'm trying acess the fields.
In my bnum.c, i have the declaration of my struct:
#include "bnum.h"
struct num {
char *vet;
int tam;
};
And in my bnum.h, I have:
typedef struct num *b_num;
And in the main file I have:
#include"bnum.h"
int main(void){
b_num b;
b->tam = 5;
I'm using gcc on Linux Mint.
The main file does not have access to the structure definition, hence the error. You should move the definition
struct num {
char *vet;
int tam;
};
from .c to .h.
This is not a good-organized code.
The struct declaration should be in the header file, this way any src file that will include the header will be
familiar with that struct.
I am trying to call a function in main.c from io.h that reads data from a file, stores that data into multiple structs, then somehow lets me pass the different structs as arguments in later functions in main. Those later functions will be defined in other files, such as alg.h.
How do I go about doing this? Do I use extern and make the structs global and put them in a separate file? Is it possible to have a function from alg.h have a return type of one of the structs? Does it depend on the order of my includes?
The code pasted below complies and works, but any attempt to move either of the structs causes the program to not compile.
Also, is it possible to have, for example, a struct declared in alg.h, then functions that have that struct as a parameter declared later in alg.h. Then in main.c, you initialize and pass the struct into a function declared in io.h, give the struct some values, have it returned to main.c, then pass that into the function declared in alg.h? I know that sounds like a class, but I need a C solution and I only need one instance of the struct floating around.
Thanks.
io.h
struct s1 {
int num1;
double num2;
};
struct s2 {
int num3;
double num4;
};
void io_init(struct s1*, struct s2*);
io.c
#include <stdio.h>
#include <stdlib.h>
#include "io.h"
void io_init(struct s1* s1i, struct s2* s2i)
{
s1i->num1 = 5;
s1i->num2 = 2.4;
FILE *fp;
char line[80];
fp = fopen("input.txt","rt");
fgets(line, 80, fp);
sscanf(line,"%i",&s2i->num3);
fgets(line, 80, fp);
sscanf(line,"%i",&s2i->num4);
fclose(fp);
}
alg.h
void ga_init(struct s1);
alg.c
#include <stdio.h>
#include "io.h"
#include "ga.h"
void ga_init(struct s1 s1i)
{
printf("%i", s1i.val1);
}
main.c:
#include <stdio.h>
#include "io.h"
#include "ga.h"
int main() {
struct s1 s1i;
struct s2 s2i;
io_init(&s1i, &s2i);
ga_init(s1i);
return 0;
}
Every file which requires the declaration of your types (i.e., wants to use them) must include your header file (ok, so forward declarations and pointers will work, but they can't be dereferenced without the definition and that's not really applicable here anyway.)
So, to elaborate, if file X needs to use struct Y then it needs to include the header file which contains its declaration, that's it.
/* X.c */
#include "Y.h" /* <-- that's it! */
void foo(Y *obj) {
/* ... */
}
Here is some advice.
Your .h file is not defining struct objects. It's just defining the type. It's fine the way it is. Everyone who touches any struct of those types should include this file.
It's very rare to need to pass a struct by value as you are doing in the call to ga_init. You will essentially always want to call by reference, like you did with io_init.
Yes, you can return a struct, but again, it would almost always be better to return a reference to a struct.
You can certainly share globally defined structs and you don't need extern unless your linker is something awful. But sharing a reference to a struct allocated in main() amounts to roughly the same thing.
I have a strange problem in C about including header files.
main.c
#include <stdio.h>
#include <stdlib.h>
#include "location.h"
int waste_new_line();
int main()
{
location *crossroads = malloc(sizeof(*crossroads));
...
location.h
typedef struct Location_Struct location;
location.c
typedef struct Location_Struct {
int ID;
char *name;
char *description;
} location;
int setup_location(location* l, char* name)
{
...
Now this isn't working because
location *crossroads = malloc(sizeof(*crossroads));
is throwing an error:dereferencing pointer to incomplete type meaning that it can see the contents of location.h, yet it doesn't seem to be aware of location.c...
I've looked around and all the tutorials I've seen say that the linker will link both files together.
EDIT:
I have altered the code to include an initializer inside location.c as so:
main.c
...
#include "location.h"
int waste_new_line();
int main()
{
location *crossroads = initialize_location();
....
location.h
typedef struct Location_Struct location;
location* initialize_location();
location.c
...
typedef struct Location_Struct {
int ID;
char *name;
char *description;
} location;
location* initialize_location(location* l)
{
return malloc(sizeof(location));
}
...
This is still throwing the same error, yet only when I try and access the members of crossroads using:
crossroads->description
this will throw the deferencing to incomplete type error.
EDIT 2: For now I've decided to just put the struct definition in the header file...
This behaviour is expected. When you #include "location.h", only the header file is visible to the compiler. The location.c file comes along later, at link time.
You have two options:
Add a function, which you declare in location.h and define in location.c, which does the necessary malloc and returns a pointer.
Move the full definition of the struct to the header file.
The main file knows about a struct called Location_Struct (and a typedef). It has no idea how big it is, thus you can't apply sizeof to it.
Since you are effectively hiding the layout and the implementation of Location_Struct it makes sense to provide a "constructor" that allocates it.
EDIT
It seems I have to mention that by "constructor" I mean an ordinary function that has access to the implementation of the structure and can allocate and possibly pre-populate the object.
You need to put the definition of Location_Struct in the header file location.h. The compiler would not "see" the other source file (unless it were #include'd, which would not typically be a good idea).