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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:41:51+00:00 2026-06-05T15:41:51+00:00

Why is it that when I define an array of floats like : const

  • 0

Why is it that when I define an array of floats like :

const int i[] = { 1, 2, 3, 4 };
float f[i[3]]; // Illegal

outside any function (i.e in global), it is illegal to do so.
while if I do the same thing inside any function (including main() ) like:

void f() {
    const int i[] = { 1, 2, 3, 4 };
    float f[i[3]];
    cout << sizeof(f);
}

main()
{
    f();
}

then everything works fine and it outputs the size as 16 successfully.
why is this difference ? Is it because of the change in the storage location from static (initially) to the stack ?

(PS: I know that in C++ an array can not be defined using a variable whose value is not known at the compile time, BUT still, then how is it working in the function ? )

  • 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-05T15:41:54+00:00Added an answer on June 5, 2026 at 3:41 pm

    By default, if you don’t specify strict standard compliance, compilers often allow C features to be used in C++ (and vice versa). Both GCC and Clang allow such a C99 feature, namely VLAs, to be used by default. (Visual Studio, OTOH, does not support VLAs even in C mode.) Note, however, that VLAs can be defined only in block scope.

    6.7.6.2 Array declarators

    2 If an identifier is declared as having a variably modified type, it shall be an ordinary
    identifier (as defined in 6.2.3), have no linkage, and have either block scope or function
    prototype scope. If an identifier is declared to be an object with static or thread storage
    duration, it shall not have a variable length array type.

    So, a global VLA does not work in C++ (with lax compiler settings) whereas a function local VLA does.

    Try compiling the following

    $ cat float.cpp
    int main() {
      int i = 2;
      const float f[i] = { 1, 2 };
    }
    

    with g++ -std=c++98 -Wall -ansi -pedantic float.cpp and you’d get something like:

    float.cpp: In function 'int main()':
    float.cpp:3:18: warning: ISO C++ forbids variable length array 'f' [-Wvla]
    float.cpp:3:29: error: variable-sized object 'f' may not be initialized
    float.cpp:3:15: warning: unused variable 'f' [-Wunused-variable]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One can easily define a function that accepts a 1d array argument like this:
i want to define an array in python . how would i do that
Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
I'm porting code that uses a very large array of floats, which may trigger
I have been trying to assign value to an object array that is defined
Why is it that for user defined types when creating an array of objects
I am developing a webservice that returns arrays of classes I define within the
I have these two simple files that define a C++ class with a tryME
How can i define that the content of the emailaddress-element has to be unquie
Let's say that I define a ClassA , that implements NSCoding protocol (this class

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.