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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:04:46+00:00 2026-05-26T08:04:46+00:00

I want to have a global names variable, which looks like that char* names[NAMES_CAP];

  • 0

I want to have a global names variable, which looks like that

char* names[NAMES_CAP];
int names_len = 0;

And I want every one who links to this library to be able to add an item to this list.

It’s easy to do that from main.

int main(int argc,char**argv) {
    names[names_len++] = "new name";
    names[names_len++] = "new name 2";
}

but what if I want to stack up two libraries? (ie, my library, libnames holds the global variable. And if someone links to libnameuser who uses libnames, it will automatically add all names defined in libnameuser to the names array in libnames.

Is there any way to do that?

In C++, I can insert the names[names_len++] = "..." to the constructor of a global object, and it must be called. But can I do that with plain C?

  • 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-05-26T08:04:46+00:00Added an answer on May 26, 2026 at 8:04 am

    Update: Refer to https://stackoverflow.com/a/2390626/270788 for updated version of this answer.

    Below is a pre-processor abstration to support C static initializer functions with GCC and MSVC. The GCC version will also work with LLVM CC, maybe some other compilers as well.

    The MSVC version works by placing a ptr to the static initializer function in a special section that is processed by the application or DLL startup code.

    #if defined(__GNUC__)
      #define INITIALIZER(f) \
        static void f(void) __attribute__((constructor)); \
        static void f(void)
    #elif defined(_MSC_VER)
      #define INITIALIZER(f) \
        static void __cdecl f(void); \
        __declspec(allocate(".CRT$XCU")) void (__cdecl*f##_)(void) = f; \
        static void __cdecl f(void)
    #endif
    
    INITIALIZER(initializer_1) { names[names_len++] = "new name"; }
    INITIALIZER(initializer_2) { names[names_len++] = "new name 2"; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

want to have a Hyperlink-Button in a gridView in which I can display a
I want to have two button in the same size one in the left
I want to have an insert tab button for my UITextView so that users
I have a module containing multiple global functions, and a global variable. The variable
I have a callback function which has the parameter const unsigned char *pData .
I have a small project that I want to deploy to both Heroku and
I have 2 classes: one is an DBobject which contains general database query and
I have following Java code: import java.io.*; class Global{ public static int a =
I've got a function that wants to access a global variable, the name of
If I have a variable, $bar , which is equal to string foo 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.