Re: dynamically allocating a 2d array



Stick to the following rule:
-Generally, an array name in an expression is evaluated as a pointer to
its first element.

Thus,
int array[Y][X] is a 2d array, an array of Y arrays of X integers.
You can write its dynamic counterpart as follows:
int (*array)[X] which is a pointer to an array of X integers.
And then allocate it dynamically via malloc:
array = malloc(sizeof(*array) * Y );

.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: difference between x[10] and (*x)[10]
    ... is converted to pointer to the first element of the array. ... array is used it "decays" to a pointer to the first element. ... conversion to this pointer added in order to maintain ...
    (comp.os.linux.development.apps)
  • Re: declaring a function that returns a pointer to 1-d array
    ... > I also want to know how can we return a pointer to first element ... > a 2d array whose elements are of type int)? ... the first element of arr is an array, not an int. ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: pointer and array help
    ... >> element of the array, which is what i expected the pointer to contain ... >> the hexadecimal value of the first element of the arrray. ... Thanks to the existence of the C-style string, ... and you passed a pointer to the first element to a method ...
    (comp.lang.cpp)