Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array size with const variable in C [duplicate]

I've found an interesting fact, and I didn't understand how is it works.
The following piece of code just works perfectly.

#include <stdio.h>
 int main(){
  const int size = 10;
  int sampleArray[size];
  typedef char String [size];
  return 0;
}

Then, I tried to use only and only the constant variable with a global scope, and it's still fine.

#include <stdio.h>
const int size = 10;
 int main(){
  int sampleArray[size];
  typedef char String [size];
  return 0;
}


But, if I change the arrays's scope to global as well, I got the following:

error: variably modified ‘sampleArray’ at file scope

#include <stdio.h>
const int size = 10;
int sampleArray[size];
typedef char String [size];
 int main(){
  return 0;
}

And I didn't get it! If I'd replace the const variable for ex. to #define it'd be okay as well.
I know that the #define variable is preprocessed, and as far as I know the const variable is only read-only. But what does make the global scope after all?

I don't understand what is the problem with the third piece of code, if the second one is just okay.

like image 501
Bálint Pap Avatar asked Nov 14 '16 22:11

Bálint Pap


People also ask

What is the size of constant variable in C?

In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof('a') is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.

Can we declare array as const?

Arrays are Not Constants The keyword const is a little misleading. It does NOT define a constant array. It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.

Can a variable be used to declared the size of array?

Declaring Arrays: This declares an array with the specified size, named variableName, of type typeName. The array is indexed from 0 to size-1. The size (in brackets) must be an integer literal or a constant variable. The compiler uses the size to determine how much space to allocate (i.e. how many bytes).

Can a variable be used to declare the size of an array in C?

Dimensions used when declaring arrays in C must be positive integral constants or constant expressions. In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared.


1 Answers

Variable Length Arrays may have only automatic storage duration. VLAs were introduced in C99.

It is not allowed to declare a VLA with the static storage duration because the size of VLA is determinated at the run time (see below)

Before this Standard you can use either a macro like

#define SIZE 10

//...

int a[SIZE];

or a enumerator of an enumeration like

enum { SIZE = 10; }

//...

int a[SIZE];

By the way you may remove the const qualifier and just write

int size = 10;

instead of

const int size = 10;

(In C++ you have to use the const qualifier though in C++ there are no VLAs except that some compilers can have their own language extensions)

Take into account that the sizeof operator for VLAs is calculated at the run-time instead of the compile-time.

like image 111
Vlad from Moscow Avatar answered Oct 25 '22 08:10

Vlad from Moscow