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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:59:22+00:00 2026-06-04T06:59:22+00:00

const int i = 100; int *j = &i; int array[i] = {0}; Is

  • 0
const int i = 100;
int *j = &i;
int array[i] = {0};

Is this legal? I read somewhere that without the &i, i and 100 would be added to the symbol-table, but because of the &i, storage is forced for i, and 100 would be stored in i at compile time; therefore the compiler would not be able to read the value of i (from storage) to allocate the array – is this true?

  • 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-04T06:59:22+00:00Added an answer on June 4, 2026 at 6:59 am

    Is this legal? I read somewhere that without the &i, i and 100 would be added to the symbol-table, but because of the &i, storage is forced for i, and 100 would be stored in i at compile time; therefore the compiler would not be able to read the value of i (from storage) to allocate the array – is this true?

    Here’s the language from the C99 standard:

    6.7.3 Type qualifiers
    …
    3 The properties associated with qualified types are meaningful only for expressions that
    are lvalues.114)


    114) The implementation may place a const object that is not volatile in a read-only region of
    storage. Moreover, the implementation need not allocate storage for such an object if its address is
    never used.

    So, sort of true, but not really relevant in this case. The compiler doesn’t have to create storage for i to use its value elsewhere; it may simply use an immediate operand with the value of 100 in place of i. Either way, the line int j = &i; is unnecessary.

    i is not an integer constant expression, meaning it’s not a compile-time constant (in C; C++ is different in this regard); it’s a run-time variable whose value may not be modified during its lifetime.

    As of C99, you can specify array sizes using a run-time variable, even one declared const, so

    const int i = 100;
    int array[i];
    

    will allocate array as a 100-element array of int. However, you cannot use an initializer with a VLA, so int array[i] = {0}; is not valid.

    Again, C++ is different, and doesn’t support VLAs at all. But since i is declared const, C++ treats it as a compile-time constant, meaning your code should be legal C++ (it builds for me, anyway).

    More from C99:

    6.7.5.2 Array declarators
    …
    4 If the size is not present, the array type is an incomplete type. If the size is * instead of
    being an expression, the array type is a variable length array type of unspecified size,
    which can only be used in declarations with function prototype scope;124) such arrays are
    nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type;
    otherwise, the array type is a variable length array type.
    …
    6.7.8 Initialization
    …
    3 The type of the entity to be initialized shall be an array of unknown size or an object type
    that is not a variable length array type.


    124) Thus, * can be used only in function declarations that are not definitions (see 6.7.5.3).

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

Sidebar

Related Questions

when doing like this: const int a = 5; I wonder if a will
I know that static const int x = 42; at namespace scope is equivalent
This compiles: int* p1; const int* p2; p2 = p1; This does not: vector<int*>
void DoWork(int n); void DoWork(const int &n); What's the difference?
I had this code: int foo(void){ return 1; } int main(void){ static const int
My problem is that i get this error binarytree.cpp: In member function ‘void BinaryTree<T>::printPaths(const
Basically the purpose of this program is to read up to 100 names from
I wrote my comparison function int cmp(const int * a,const int * b) {
I have these strings: int name const int level const char * const format
I'm a bit confused what happened in the following code: const int e =

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.