Dereferencing pointer to incomplete type C - c

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.

Related

Understanding structs in C

I just started learning C.
I have the following code:
#include <stdio.h>
void func(struct Foo foo){
foo.x =1;
foo.array[3] =2;
}
int main(){
struct Foo lol;
lol.x = 55;
lol.array[3] = 67;
func(lol);
printf("lol.x is %d and lol.array[3] is %d\n", lol.x, lol.array[3]);
return 0;
}
But when I compile it I get the following errors
structs.h:3:22: error: variable has incomplete type 'struct Foo'
void func(struct Foo foo){
structs.h:10:13: error: variable has incomplete type 'struct Foo'
struct Foo lol;
I looked it up and a lot of times the problem is in not defining structs in .h file. I defined it in a separate .h file, didnt help. I defined structs on the top of .c file, same errors.
I added this to the top of the file:
typedef struct Foo{
int x;
int array[3];
}Foo;
Can someone explain why its happening?
I looked it up and a lot of times the problem is in not defining structs in .h file. I defined it in a separate .h file, didnt help.
If you defined struct in separate .h file that you should include that file in this code.
#include <stdio.h>
#include "your_file.h"
....
From edit some other problem (UB)-
foo.array[3] =2; //you can't access index 3, declaration in struct is int array[3].
You are seeing a compilation time error, since struct Foo foo cannot evaluate the type. You have to declare the structure before using it.
You can include the header file or just write the following lines above func definition.
struct FOO
{
int x;
int array[n]; // n>3
};

error: dereferencing pointer to incomplete type in main file

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);
}

how can I use a pointer to global struct in ansi-c?

Here is the case. In the file "fileA.c" I have
typedef struct MY_STRUCT
{
int A;
int B;
int C;
}MY_STRUCT;
MY_STRUCT Data;
/* Function */
int function(MY_STRUCT *params)
{
int varA, varB, varC;
varA = params->A;
varB = params->B;
varC = params->C;
}
And I need to fill the struct elements from other routine, for instance, "fileB.c" which contains the following:
extern MY_STRUCT Data;
int function(MY_STRUCT *params);
/* Function */
void userMain(void)
{
Data.A = 1254;
Data.B = 5426;
Data.C = 1236;
function(&Data);
}
But I'm getting the error:
"[Error] fileB.c E208: syntax error - token ";" inserted before "Data"
And whe I cross probe the error the compiler take me to the declaration "extern MY_STRUCT Data;"
So my question is how do I accomplish this functionality? I mean, how do I fill the elements of the structure from another function in another file different from the file where I declared the struct?
When the compiler is compiling fileB.c, it doesn't know about the typedef that you've defined in fileA.c. So in fileB.c, MY_STRUCT is an unknown type.
You should move the typedef to a common header, and include it in fileA.c and fileB.c.
Elaborating a bit on #pb2q answer:
Create a filea.h file with (omitting the defines and stuff):
struct MY_STRUCT
{
(blah blah blah ...)
};
extern MY_STRUCT Data;
This will declare the struct and tell whoever wants to know that the variable is declared in another file. Then put in filea.c the following lines
#include "filea.h" // near the top
(...)
MY_STRUCT Data; // Somewhere meaningful
This will actually declare the variable Data. Finally, in file "fileb.c" type
#include "filea.h"
that allows you to use the variable Data.

C - Dereferencing pointer to incomplete type

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

How to access a structure declared in other file using extern keyword in 'c' language

I have 3 files . In one file I declared a structure and in another file which has the main, I am trying to access that structure using extern key word --------
//a.c---
include<stdio.h>
extern struct k ;
extern int c;
int main()
{
extern int a,b;
fun1();
fun2();
c=10;
printf("%d\n",c);
struct k j;
j.id=89;
j.m=43;
printf("\n%d\t%f",j.id,j.m);
}
//1.c
#include<stdio.h>
struct k
{
int id;
float m;
}j;
int c;
void fun1()
{
int a=0,b=5;
printf("tis is fun1");
printf("\n%d%d\n",a,b);
}
//2.c--
#include<stdio.h>
struct k
{
int id;
float m;
}j;
void fun2()
{
int a=10,b=4;
printf("this is fun2()");
printf("\n%d%d\n",a,b);
}
I compiled this code by using cc a.c 1.c 2.c
but I am getting error as storage size of ‘j’ isn’t known
//a.h---
#include<stdio.h>
#include "1.h"//cannot know its there without including it first.
#include "2.h"
extern struct k;// don't really need to do this and is wrong.
extern int c;
//a.c
int main()
{
extern int a,b;//externs i believe should be in the h file?
fun1();
fun2();
c=10;
printf("%d\n",c);
struct k *ptr = malloc(sizeof(struct k));//Define our pointer to the struct and make use of malloc.
//now we can point to the struct, update it and even retrieve.
ptr->id = 89;
ptr->m = 43;
printf("\n%d\t%f" ptr->id,ptr->m);
}
//1.h
#include<stdio.h>
typeof struct k
{
int id;
float m;
}j;
//1.c
int c;
void fun1()
{
int a=0,b=5;
printf("tis is fun1");
printf("\n%d%d\n",a,b);
}
//2.h--
#include<stdio.h>
struct k
{
int id;
float m;
}j;
//2.c
void fun2()
{
int a=10,b=4;
printf("this is fun2()");
printf("\n%d%d\n",a,b);
}
I have edited your code in places so it should see the struct and point to it. Each C file should know have an header h file. When the a.h belonging to your main includes files not only it can see them but should be able to access them. This means it should also know what K is J is alias of K if I remember correctly.
I should know update the struct and retrieve data from it via pointer. If this still doesn't work please post your compiling error and copy n paste the line its crying about.
You have no definition for struct k visible from a.c. Put the definition in a header file and include it from all three source files.
struct k is a description of how to build an object. It is not an object. extern operates on objects.
Usually struct blocks are placed in a header, or .h file.
j is an object of type k. It should be declared extern in either 1.c or 2.c.
Functions which are used among multiple files, like variables, should be declared before use.
Putting it all together, you might want
//a.c---
#include <stdio.h>
#include "k.h" /* contains definition of struct k */
extern int c;
extern k j;
extern void fun1();
extern void fun2();
int main()
{
extern int a,b;
fun1();
fun2();
c=10;
printf("%d\n",c);
j.id=89;
j.m=43;
printf("\n%d\t%f",j.id,j.m);
}
Some of your concepts are wrong. Please check this link.
To access a variable as extern you have to first define it, which you have not done in your case.
The extern modifier changes the linkage of the variable that you are declaring or defining. In C, only variable names can have linkage - not types, like struct k.
If you wish to access the internals of a struct type, it must be fully defined in the same source file. The usual way to accomplish this for types that are shared between source files is to place the definition of the struct into a header file, which is included with #include into each source file.

Resources