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

  • Home
  • SEARCH
  • 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 8007687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:54:48+00:00 2026-06-04T17:54:48+00:00

First, see this example (I made this up for the sake of example, it’s

  • 0

First, see this example (I made this up for the sake of example, it’s not a real program):

whatever.h

#ifndef WHATEVER_H
#define WHATEVER_H

void fill(void);

#endif

main.c

#include <stdio.h>
#include "whatever.h"

char *names[10] = {NULL};

int main()
{       
        int i; 
        fill(); 
        for (i = 0; i < 10; ++i)
                printf("%s\n", names[i]);
        return 0;
}

whatever.c

#include "whatever.h"

extern char **names;

void fill(void)
{
        int i;
        for (i = 0; i < 10; ++i)
                names[i] = "some name";
}

When I make this program using:

gcc -o test main.c whatever.c -Wall -g

I don’t get any errors or warnings. However, when I run the program, I see that in fill, names is actually NULL. If in whatever.c I change

extern char **names;

to

extern char *names[];

then everything is fine.

Can anyone explain why this happens? If gcc couldn’t link extern char **names; with the one in main.c, shouldn’t it have given me an error? If it could link them, how come names ends up being NULL in whatever.c?

Also, how does extern char **names; differ from extern char *names[];?


I am using gcc version 4.5.1 under Linux.

Update

To further investigate this, I change the definition of names in main.c to:

char *names[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

(keeping extern char **names; in whatever.c) and with gdb, I can see that names has a value. If I cast that value to char * and print it, it gives me "1". (Note that, it’s not *names that is "1", but (char *)names)

Basically, what it means is that gcc has somehow managed to link extern char **names; in whatever.c with names[0] in main.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-06-04T17:54:49+00:00Added an answer on June 4, 2026 at 5:54 pm

    Whenever you use different, incompatible types for the same variable in different compilation units (as you have done here), you get undefined behavior. That means it probably won’t work and you may not get any error or warning messages about it.

    WHY this happens (and why the spec says this is undefined) is due to the way most linkers work. Most linkers don’t understand anything about types; they only understand names and memory. So as far as the linker is concerned, a variable is just a block of memory starting at some address. In your (original) program main.c defines names as referring to the start of a block of memory big enough to hold 10 pointers (probably 40 or 80 bytes, depending on whether this is a 32-bit or 64-bit system), all of which are NULL. whaterver.c, on the other hand, assumes that names refers to a block of memory big enough to hold ONE pointer, and that pointer points at an array of 10 pointers.

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

Sidebar

Related Questions

First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
First of, please check this picture: As you can see, left from Today and
All I need this for is strcpy(). I want to see whether the first
It is not the first time i see it Ive seen in Facebook source
I made my first jQuery plugin attempt, but I have this problem/bug which I
Following this answer , I made a simple example to be sure I properly
I'm just taking my first steps with Azure and the first thing I see
I'm familiarizing myself with Ruby and it's the first time I see synonyms for
Please see following examples first, and which one is better? Could you compare some
I see time.clock() on Windows 'starts' a timer when called for the first time,

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.