1

I'm trying to program up a 2D dynamically allocated array for a map for a small game. Below is my code, I'm unsure why int* array[yCoord] is throwing up the error that it cannot be a variable length array, as the variable yCoord is assigned a specific value in the main function below. Any help greatly appreciated.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"random.h"

void createMap(int xCoord, int yCoord) {
    int i, j;
    xCoord += 2;
    yCoord += 2;
    int* array[yCoord];
    for (i = 0; i < yCoord; i++) {
        array[i] = (int*)malloc(xCoord * sizeof(int));
    }
    for (i = 0; i < yCoord; i++) {
        for (j = 0; j < xCoord; j++) {
            array[i][j] = 0;
        }
    }
}

int main(void) {
    int x, y;
    x = 5;
    y = 5;
    createMap(x, y);
    return 0;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
tomparko
  • 43
  • 6
  • 1
    What compiler are you using? What version of the compiler? What options, flags and arguments do you pass to the compiler? It seems that you use a very old compiler, or pass flags that tells it to use a very old C standard, where variable-length arrays are not allowed. – Some programmer dude Sep 16 '22 at 06:20
  • 1
    `int* array[yCoord];` is a variable-length array (because `y` is not known at compile time). These are only supported by C99 (and only optional). You need to dynamically allocate the array – Raildex Sep 16 '22 at 06:22
  • @Someprogrammerdude -ansi -pedantic -Wall -Werror is what I compile with. This is for a programming assignment at University, they insist we code in C89 therefore it is quite old. – tomparko Sep 16 '22 at 06:31
  • 3
    Well as mentioned [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) was added in the C99 standard, they simply doesn't exist in C89. You need to dynamically allocate the whole array itself as well. – Some programmer dude Sep 16 '22 at 06:33
  • This is probably just a part of your code, to show the error, but pay attention to the createMap function use of stack variables and calls to malloc. You dont want to leak memory, could be good to run your code through Valgrind before submitting the assignment. – Eben Sep 16 '22 at 06:36
  • 1
    @tomparko So you aren't using Visual C. Then why did you add that tag to your post? – Lundin Sep 16 '22 at 06:41
  • @Lundin apologies, I meant to tag C90 not vc90 – tomparko Sep 16 '22 at 06:43
  • 1
    The program can be replaced with `int main(){}` since it does nothing observable. – n. m. could be an AI Sep 16 '22 at 07:00
  • @n.1.8e9-where's-my-sharem. Are you sure that calling malloc doesn't count as a side effect? – Lundin Sep 16 '22 at 08:38
  • BTW, are you sure that you *need* to *dynamically* allocate the map? If the size is known and fixed you could just use an actual 2D array. – Bob__ Sep 16 '22 at 08:43
  • @Lundin [gcc](https://godbolt.org/z/MKfn8ndT9) seems happy to ignore the code. I'm a bit surprised. Just a bit. – Bob__ Sep 16 '22 at 08:50
  • also [don't cast the result of malloc in C](https://stackoverflow.com/q/605845/995714) – phuclv Sep 16 '22 at 09:28

1 Answers1

1

int* array[yCoord]; is a variable-length array (VLA) since the dimension is determined in run-time based on another variable. And in this case also highly questionable since that array will only exist inside that function, you can't return it to the caller. Also please note that bad teachers/books have a habit of teaching you how to malloc 2D arrays incorrectly - Correctly allocating multi-dimensional arrays (please share with your teacher if they don't know this).

VLA were introduced to the C language in the year 1999. So if you are using a compiler older than 23 years, or explicitly telling one to compile using an obsolete version of the language, the code won't compile. Hence "ISO C90 forbids variable length (C89/C90)" - your compiler is using the long-since obsolete version of C from 1989.

I would strongly recommend to upgrade to a compiler from this millennium. In case of old versions of gcc, it means executing them correctly since it defaulted to C90 (or rather "GNU90") all the way to version 5.0.0. See What compiler options are recommended for beginners learning C? As noted there, the -ansi switch is harmful, since it does not mean "strict standard compliance" but rather "standard compliance against ANSI/ISO 1989". Switch -ansi for -std=c99, -std=c11 or -std=c17 (the current version of C).

Lundin
  • 195,001
  • 40
  • 254
  • 396