Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:31:11+00:00 2026-06-13T21:31:11+00:00

I am trying to call a function in main.c from io.h that reads data

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:31:12+00:00Added an answer on June 13, 2026 at 9:31 pm

    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) {
        /* ... */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a function in a Python script from my main C++
I'm trying to call a function called animateRight in a file called ScanAnimation.m from
I'm trying to call a function from a form within the same .php file,
I'm trying to get some data from a file, then parse it and pass
I am trying to basically call a function from another javascript file (inside a
I am trying to create a simple phonebook program that reads data from a
I am trying to do the following: function main(callback) { $.ajax('server-side', function() { this.callback.call(hello);
I'm trying to call a function present in one class from another class by
I am trying to call c# function from javascript the code i have tried
I am trying to call a function, but am running into some struggles with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.