using struct elements in function that is in another source file - c

New to learning c and I am having difficulties with writing a function in another source file that uses struct variables from the first source file.
basically I need to create a bubblesort function in another file to be used in my driver
this is what i am trying to do
driver.c
#include<stdio.h>
#include<stdlib.h>
#include<sort.c>
typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;
int main(){
sort();
}
sort.h
void sortList(IORB * head, int(*prio)(int));
sort.c
void sortList(IORB * head, int(*prio)(int)){
// do stuff
}
but i get this error:
unknown type IORB in the sort function.
how can pass the IORB to the function ?
Updated code after trying the answer:
driver.c
#include<stdio.h>
#include<stdlib.h>
#include "sort.h"
#include "iorb.h"
//removed the typedef in the driver
iorb.h
#ifndef IORB_H
#define IORB_H
typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;
#endif
sort.h
#ifndef SORT_H_
#define SORT_H_
void sortList(IORB * head, int(*prio)(int));
#endif
sort.c
#include "sort.h"
#include<stdlib.h>
#include<stdio.h>
#include "iorb.h"
void sortList(IORB * head, int(*prio)(int)){
//do stuff
}

You want to put your type declaration / typedef in a header file:
#ifndef IORB_H
#define IORB_H
typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;
#endif
Then include that header file in sort.h:
#include "iorb.h"
and you want to include the sort.h in your sort.c:
#include "sort.h"

Related

How to forward declare a typedef structure in C which has no tag

file1.h: Library code. CANT change it
#ifndef FILE1_H_
#define FILE1_H_
typedef struct
{
int x;
} MY_STRUCT;
#endif /* FILE1_H_ */
file2.h: user defined. can be changed. How to forward declare above typedef struct which has no tag ?
#ifndef FILE2_H_
#define FILE2_H_
struct MY_STRUCT;
void print(struct MY_STRUCT * obj);
#endif /* FILE2_H_ *
file2.c: user defined. can be changed
#include "file2.h"
#include "file1.h"
#include <stdio.h>
void print(struct MY_STRUCT * obj)
{
printf("x: %d", obj->x);
}
main.c
#include "file2.h"
#include "file1.h"
int main(void){
MY_STRUCT obj1;
obj1.x = 100;
print(&obj1);
}
The code can be seen here. https://paiza.io/projects/wa2PCvUswWyyAzdxxjggxQ?language=c
It's not possible to "forward-declare" a typedef . You will have to give the struct a tag, e.g.:
typedef struct my_struct MY_STRUCT;
// ... later
struct my_struct
{
int x;
};

How to use structure from one c file in another [duplicate]

This question already has answers here:
How to use a defined struct from another source file?
(7 answers)
Closed 5 years ago.
is it posibble in C, that you define a structure in one .c file, but use it in another?
Basically, I would like to use my List that I have already created in another program. But I would like to use diffrent structures.
I've 3 files:
main.c -- another program, where I want to use the list
list.c -- code of the list
head.h -- header to bind them
In main.c:
#include <stdio.h>
#include <stdlib.h>
typedef struct cell{
int x;
cell next;
}TCell;
typedef struct{
TCell first;
int lenght;
}List;
#include "head.h"
int main()
{
TCell *c
c->x = 5;
List *l;
init(l);
add(l,c)
c = get();
return 0;
}
head.h:
#ifndef HEAD_H_
#define HEAD_H_
void init(List *l);
void add(List *l, TCell *c);
TCell get();
(...)
#endif
list.c
#include <stdio.h>
#include <stdlib.h>
#include "head.h"
typedef struct{
TCell first;
int lenght;
}List;
void init(List *l){ (...) }
void add(List *l, TCell *c) { (...) }
TCell get() { (...) }
(...)
But when i try to compile it, it doesn't work, because it is missing the TCell in head.h and list.c, and the List in head.h
So, is there a possibility to have just one definition of TCell in the main.c, so whenever i change its inner variables, it will still work?
Make this your head.h:
#ifndef HEAD_H_
#define HEAD_H_
typedef struct cell
{
int x;
cell next;
} TCell;
typedef struct
{
TCell first;
int lenght;
} List;
void init(List *l);
void add(List *l, TCell *c);
TCell get();
...
#endif
And remove the typedef of List and TCell from list.c and main.c, because you only need to define a type once.
Header files are meant to contain your shared type definitions.
Here's an entry of the famous C-FAQ that may help. I advise you to read through this whole FAQ it will teach you a lot of things about C and will save you a lot of time.

Passing a typedef from header to source - C

I have the following main.c file:
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include "lista.h"
int main(int argc, char *argv[])
{
struct nod *root = NULL;
root = init(root);
return 0;
}
And lista.h:
#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED
#include "lista.c"
typedef struct nod
{
int Value;
struct nod *Next;
}nod;
nod* init(nod *);
void printList(nod *);
#endif // LISTA_H_INCLUDED
And finally lista.c which is:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include "lista.h"
nod* init(nod *root)
{
root = NULL;
return root;
}
void printList(nod *root)
{
//We don't want to change original root node!
nod *aux = root;
printf("\n=== Printed list =====\n");
while (aux != NULL)
{
printf(aux->Value);
aux = aux->Next;
}
puts("\n");
}
Even after included header file, I'm getting three errors which say:
Unknown type name 'nod'
How to make that typedef from lista.h to be seen on lista.c?
I just can't figure out what is happening here.
Have a look at your lista.h header file:
#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED
#include "lista.c"
[..]
#endif // LISTA_H_INCLUDED
You're including lista.c, which you shouldn't do at all. And the error occures, because at that time nod isn't defined yet.

C - redefinition error in Xcode

My c header file has the following error message in Xcode
Redefinition of 'entry'
But it works perfectly when I compile it using gcc in command line. Could any of you give an explanation of why?
This is snapshot.h:
#ifndef SNAPSHOT_H
#define SNAPSHOT_H
#define MAX_KEY_LENGTH 16
#define MAX_LINE_LENGTH 1024
typedef struct value value;
typedef struct entry entry;
typedef struct snapshot snapshot;
struct value {
value* prev;
value* next;
int value;
};
// the line below is where the redefinition error appears
struct entry {
entry* prev;
entry* next;
value* values;
char key[MAX_KEY_LENGTH];
};
struct snapshot {
snapshot* prev;
snapshot* next;
entry* entries;
int id;
};
#endif
This is snapshot.c:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "snapshot.h"
int
main(int argc, char *argv[]){
int x = 7;
printf("x= %d\n" , x);
printf("value = %d\n", 1);
return 0;
}
entry was originally reserved as a keyword and then later declared obsolete. So older compilers don't allow it (see this question). Change the name of the struct and everything should be fine.

In C, how do you use a struct in the main function that was declared in a header file?

In C, I need to declare a struct linked list in a header file. Within the main of my .c file, how do I declare a new instance of my struct? Also should I use typedef for my struct?
In your header:
struct mystruct
{
int a;
int b;
};
in .c file include (#include "header.h") the header, then use in main:
struct mystruct obj1;
obj1.a=0;
obj1.b=0;
In your header file add the definition and typedef
typedef struct linked_list_node_st {
int val;
struct linked_list_node_st *next;
} Linked_List_Node;
In main.c, call the struct using the typedef.
#include <stdio.h>
#include <stdlib.h>
#include "structure.h"
int main(int argc, const char * argv[]) {
Linked_List_Node *node;
node = malloc(sizeof(Linked_List_Node));
node->next = 0;
node->val = 1;
return 0;
}
U have struct in header, and want that struct in main.c?
Just include it with #include "header.h" directive in your main.c, and start to use struct.
You use typedef when u want to make "your variable" in c, like:
typedef int INTEGER;
INTEGER number;
So u use typedef for closer indentify your struct, for you or for readers.

Resources