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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:40:48+00:00 2026-05-24T05:40:48+00:00

There are two source files in my program. An array is defined in A.cpp

  • 0

There are two source files in my program.

An array is defined in A.cpp.

// compiler: MSVC2005 SP2    
// A.cpp

// defines an array of type "int [100]"
int a[100] = {3};

It is used in B.cpp.

// B.cpp

// declares an array of type "int []"
extern int a[];

int main()
{
  // prints 3 correctly
  cout << a[0] << endl;
  return 0;
}

AFAIK, linker will raise an error if it cannot find any matched definition for a declaration if the declared identifier is used. Here, int [] and int [100] are two different types, obviously.

Why, in this case, isn’t there any link error? Is it guaranteed by the Standard that array size is trivial during matching of declaration/definition? Or it’s just implementation-specific? A quote from the Standard will be appreciated if any.

Edit: iammilind mentioned in his answer that linker can run correctly(his compiler is gcc) even if the type does NOT match between declaration and definition. Is it REQUIRED by the Standard or just a way of gcc? I guess this is a far more important issue to figure out.

  • 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-24T05:40:49+00:00Added an answer on May 24, 2026 at 5:40 am

    In both C and C++ a declaration of an object a of incomplete type will match the definition of object a where the type is complete. What you observe simply illustrates the fact that in C++ you are allowed to use incomplete types in non-defining declarations. But once you get to the definition the type must be complete.

    This behavior is not restricted to arrays. For example, you can declare

    extern class X x;
    

    for a totally unknown class X, and then, when class X is already fully defined, you can define

    X x;
    

    which will match to the above declaration.

    The same thing happens with your array. Firstly you declare an object of incomplete type

    extern int a[];
    

    and then you define it with complete type

    int a[100];
    

    The types here indeed are not matching. However, the C++ language never required them to match. For example, 3.9/7 explicitly states

    The declared type of an array object might be an array of unknown size
    and therefore be incomplete at one point in a translation unit and
    complete later on; the array types at those two points (“array of
    unknown bound of T” and “array of N T”) are different types.

    It means that the same array object can have incomplete type initially, but acquire a complete type later on. (See also the example in 3.9/7). This doesn’t, of course, mean that you can declare a as an int and then define it as a double. The only type-related freedom you have here is, again, to complete an incomplete type. No more.

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

Sidebar

Related Questions

Say your program is composed of two source files (main.c and auxiliary.c) and two
These two files are mostly seen in open source projects. What are they for,
The facts: When a file is moved, there's two possibilities: The source and destination
I would like to compare two xml files using some kind of program (
I've been staring at the source code of these two download pages for awhile
Given two interface references obtained from different sources. Is there a programmatic way to
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There are two popular naming conventions: vc90/win64/debug/foo.dll foo-vc90-win64-debug.dll Please discuss the problems/benefits associated with
There are two scenarios I need to clarify: An executable compiled with .NET 3.5

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.