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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:23:55+00:00 2026-05-26T02:23:55+00:00

How to create an external character array in C? I have tried various ways

  • 0

How to create an external character array in C?

I have tried various ways to define char cmdval[128] but it always says undefined reference to 'cmdval'

I want to put a string in cmdval in first.c file and use it in other second.c file. I tried adding a global.h file with extern char cmdval[128] but no luck.

UPDATE:

global.h

extern char cmdval[128];

first.c

#include "global.h"

char cmdval[128];

function(){
   strcpy(cmdval, "a string");
}

second.c

#include "global.h"

function(){
   printf("%s \n",cmdval); //error
}

FAIL 🙁 “undefined reference to `cmdval'”

EDIT:
I am working in linux (editing a mini OS xv6 then compiling and running it in qemu), I don’t know if it is a barrier

  • 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-26T02:23:56+00:00Added an answer on May 26, 2026 at 2:23 am

    You need to declare it in the .h file

    extern char cmdval[128];
    

    And then define the value in first.c;

    char cmdval[128];
    

    Then anything that includes your .h file, provided it is linked with first.o will have access to it.

    To elaborate, “extern” is saying, there is an external variable that this will reference… if you dont then declare cmdval somewhere, cmdval will never exist, and the extern reference will never reference anything.

    Example:

    global.h:

    extern char cmdval[128];
    

    first.c:

    #include "global.h"
    char cmdval[128];
    
    int main() {
      strcpy(cmdval, "testing");
      test();
    }
    

    second.c:

    #include "global.h"
    
    void test() {
      printf("%s\n", cmdval);
    }
    

    You can compile this using:

    gcc first.c second.c -o main
    

    Or make the .o files first and link them

    gcc -c first.c -o first.o
    gcc -c second.c -o second.o
    gcc first.o second.o -o main
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to create a transactional process using an external API that
I want to create file-links to open an external editor from firefox. I have
I am trying to create an application that makes a window (external to the
I need to create a page that will load divs from an external page
I create a custom import and export, at the moment as an external script
I'm trying to create a custom file/check in check out script for external hardrives,
I work a lot with external Web Services, and I need to create an
I can create function pointers in Fortran 90, with code like real, external ::
I'm writing a web app that points to external links. I'm looking to create
I have made an actionscript that loads an external text file, and scrolls its

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.