-1

Hey I'm working on a problem and here is what I have to do:-

Write a function called initarray that takes an array of pointers to int and an int representing the size of the array, as arguments. The function should initialize the array with pointers to ints (use malloc) that have a value corresponding to the array indices at which a pointer to them are stored (the pointer stored at array index 2 should point to an integer with a value of 2).

So far I've written this, but It's giving me an error "[Error] variable-sized object may not be initialized" Can you tell me what I'm doing wrong here?

#include<stdio.h>
void initArray(int **a, int sz){
int i;
for (i = 0; i < sz; i++) {
a[i] = calloc (1, sizeof **a);
 *a[i] = i; 
    }
  } 
 int main(){
 const int Var = 10;
 int *array[Var] = {NULL};
 initArray(array,3);
 }
Noob123
  • 1
  • 1

3 Answers3

2

For historical reasons, the value of a const variable is never considered a constant expression in C.

So if you use it as an array dimension, then the array is a variable-length array, and variable-length arrays are not allowed to have initializers.

One solution not mentioned yet is to use enum. Enumerators are in fact constant expressions, and they don't suffer from the same "bigger hammer" issue as preprocessor macros:

int main()
{
    enum { Var = 10 };
    int *array[Var] = {NULL};
    initArray(array,3);
}
M.M
  • 138,810
  • 21
  • 208
  • 365
1

C has no symbolic constants with user-defined type. You encountered one of the differences to C++.

The const qualifier just is a guarantee you give to the compiler you will not change the variable(!) Var.

Arrays with initialiser and global arrays require a constant expressing which can be evaluated at compile-time. As Var is semantically still a variable, you cannot use it.

The C-way to emulate symbolic constants are macros:

#define ARRAY_SIZE 10

...

// in your function:
int *array[ARRAY_SIZE] = ...

Macros are handled by the preprocessor and are a textual replacement before the actual compiler sees the code.

Note I changed the name to a more self-explanatory one. The macro should also be at the file-level, typically near the beginning to allow easier modifications. Using the integer constant 10 directly in the code is a bad idea. Such magic numbers are often cause of errors when a modification is required.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
-1

The error would suggest that you can't use an initializer (the = {NULL} in your main function) on a variable-sized object. While it looks like it isn't variable (because of the const on Var, and because 10 is a constant) it sees it as variable because you're accessing it through a variable. If you use:

int *array[10] = {NULL}

I think your snippet will work fine.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
  • Glad I could help! Don't forget to accept this answer using the green check mark on the left. Welcome to Stack Overflow! – Haldean Brown Apr 07 '16 at 23:53
  • 1
    "it sees it as a variable because of the indirection" - What does that mean? There is no indirection using `Var`. and even with, this would not change its role as a variable. – too honest for this site Apr 08 '16 at 00:05
  • 1
    Actually, the compiler sees the array declaration as variably modified because `Var` is not an integer constant (it's not a constant expression), even though it is the name of a `const`-qualified integer variable. – Jonathan Leffler Apr 08 '16 at 00:09
  • 1
    There's "indirection" and there's "indirection". I was using it in the general "it is not direct" meaning of the term, where even though "10" is constant you are accessing the 10 indirectly through a variable. You're right that I could have phrased that in way that did not conflict with a word that has a meaning in C as well. I'll reword the answer. – Haldean Brown Apr 08 '16 at 00:45