phanitej
phanitej

Reputation: 113

passing structure to a file in c

I've seen some answers on StackOverflow regarding this. In my c project, I am using the main function and a child function. The child function is written in a separate .c file, with its header file included in the main file.

#include<stdio.h>


struct student
{
    int t1;
    float e1;
};

typedef struct student stu;

#include"struct_demo.h"

void main()
{
    stu s1;
    stu *r1=&s1;

    s1.t1=10;
    s1.e1=172.1;

    struct_demo(r1);
}

And the function struct_demo.c is as follows

#include"stdio.h"

void struct_demo(stu *s1)
{

    s1->e1=9;
    printf("%d",s1->e1);

}

The header file for the function struct_demo is

#ifndef STRUCT_DEMO_H_
#define STRUCT_DEMO_H_

void struct_demo(stu *s1);

#endif /* STRUCT_DEMO_H_ */

My compiler is showing errors in the child function

expression must have pointer-to-struct-or-union type

identifier stu is undefined

The same program when executed without the use of separate .c files (with functions written in a single .c file under separate function) works. Where am I going wrong?

Upvotes: 0

Views: 328

Answers (2)

Spark_TheCat
Spark_TheCat

Reputation: 89

for parsing the address you need to do

myStructure* myFunction(structure* myStructure)
{
    //some opperation
    return myStructure;
}

this way you are parsing the address of that structure but be careful when initializing it you will have to use calloc to make room in memory for it because at this point that pointer can go anywhere.

Upvotes: 0

Deepak Kumar T P
Deepak Kumar T P

Reputation: 1076

struct student
{
    int t1;
    float e1;
};

typedef struct student stu;

Move this code struct_demo.h since the struct_demo.h doesn't know what is stu

Upvotes: 1

Related Questions