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 8619631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:23:13+00:00 2026-06-12T06:23:13+00:00

Lets say I have a header file foo.h which declares a struct variable #include

  • 0

Lets say I have a header file foo.h which declares a struct variable

#include <stdio.h>

typedef struct foo_struct foo_s;

foo_s foo_function(int a);
foo_s bar_function(int b);

and a source file foo.c which actually defines the structure

#include "foo.h"

struct foo_struct
{
   int a;
   int b;
};

foo_s foo_function(int a)
{
    foo_s fs;
    fs.a = a;
    return fs;
}

Now I want to access the structure defined in foo.c in another source file bar.c so I try the following:

#include "foo.h"

foo_s bar_function(int b)

{
  foo_s fb;
  fb.b = b;

  return fb;
}

…and fail with bar.c:3:7: error: return type is an incomplete type

I sort of understand what the problem is but is there a workaround?

  • 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-12T06:23:14+00:00Added an answer on June 12, 2026 at 6:23 am

    You’re breaking your abstraction by needing to know the definition of struct foo_struct outside of foo.c. The whole point of making a struct definition “private” to a particular source file is so that other source files aren’t aware of and cannot manipulate the members of struct foo_struct directly.

    You either need to move bar into foo.c, or you need to put the struct definition in foo.h (making it public), or you need to define an interface that allows other translation units to allocate, set, read, and deallocate items of type foo_s without exposing the details of its type, similar to how the routines in stdio.h manipulate objects of type FILE, something like

    foo_s *create_foo(int a, int b);
    void set_foo(foo_s *f, char *property, int value);
    int get_foo(foo_s *f, char *property);
    void destroy_foo(foo_s **f);  // sets f to NULL after deallocation.
    

    You would add the above interface to foo.h and implement it in foo.c; functions in other translation units (source files) would use it like:

    void blah(void)
    {
      foo_s *f = create_foo(0,0);
      if (f)
      {
        set_foo(f, "a", 1);
        set_foo(f, "b", 2);
    
        printf("a = %d, b = %d\n", get_foo(f, "a"), get_foo(f, "b");
        destroy_foo(&f);
        assert(f == NULL);
      }
    }
    

    There are probably a hundred better ways to do that, but you should get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have two inline functions in my header file: inline int foo()
Lets say that I have a header file a.hxx which includes b.hxx and c.hxx.
I have a header file, lets say Common.h, that is included in all of
Let's say I have a header file called inclusions.h that has all the #include
Let's say i have a header file Snake.h: #include SnakeBodyPart.h #include GUI.h //... And
Lets say I have a file with a .quote extension. I'd like the web
If I have a several header files :lets say 1.h , 2.h , 3.h
I have the following files. foo.h (C++ header file) foo.mm (C++ file) test_viewcontroller.h (objective
Let's say I have a header file with a class that uses std::string .
So lets say i have a php file that contains $title=Website | Categories; And

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.