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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:58:47+00:00 2026-06-12T17:58:47+00:00

Consider the following struct defined in ModuleA: typedef struct{ int A; int B; int

  • 0

Consider the following struct defined in ModuleA:

typedef struct{
    int A;
    int B;
    int C[4];
}myStructType;

myStructType MyStruct;

If I wanted to use this struct from ModuleB, then I would declare the struct in the ModuleA header like this:

extern myStructType MyStruct;

So far, so good. Other modules can read and write MyStruct by including the Module A header file.

Now the question:

How can I declare only part of the struct in the Module A header file? For example, if I wanted ModuleB to be able to read and write MyStruct.C (or, to make things a bit easier, perhaps MyStruct.A or MyStruct.B), but not necessarily know that it’s in a struct or know about elements A and B.

Edit: I should probably also specify that this will go in an embedded system which does basically all of its memory allocation at compile time, so we can be extremely confident at compile time that we know where MyStruct is located (and it’s not going to move around).

Edit2: I’ll also clarify that I’m not necessarily trying to prevent other modules from accessing parts of the struct, but rather, I’m attempting to allow other modules to access individual elements without having to do MyStruct.Whatever because other modules probably only care about a single element and not the whole structure.

  • 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-12T17:58:48+00:00Added an answer on June 12, 2026 at 5:58 pm

    You would have to encapsulate it, i.e. make a private variable such as:

    static myStructType the_struct;
    

    in some C file, and then provide an API to get access to the parts:

    int * getC(void)
    {
      return the_struct.C;
    }
    

    this would then let other C files get access to an integer array by calling

    int *some_c = getC();
    some_c[0] = 4711;
    

    or whatever. It can be made “tighter” by being more explicit about the length of the returned array of course, I aimed for the minimal solution.

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

Sidebar

Related Questions

Consider the following: typedef struct { int a; int b; int c; int d;
Consider the following example: struct MyStruct { int a; int b; }; I can
Consider the following: struct A { typedef int foo; }; struct B {}; template<class
Consider the following two FFI structs: class A < FFI::Struct layout :data, :int end
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Java noob question: Consider the following C array and initializer code: struct { int
Consider the following struct: public struct vip { string email; string name; int category;
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Consider the following C++ program struct str { int mem; str() try :mem(0) {
The following code is illegal: public struct MyStruct { public MyStruct(int a, int b)

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.