0

I'm trying to do a function in C that takes a matrix as an argument.
I will know only in run time how many elements will the matrix have (However is a NxN matrix).

Therefore I want to know if there is some warranty that the space will ALWAYS be contiguous and thus I can only ask the pointer to the first element. I declared my matrix like this:

int nodo;
scanf("%d", &nodo);
int distancias[nodo][nodo];
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Baxter Lopez
  • 1,110
  • 1
  • 9
  • 14
  • 5
    c arrays are contiguous. – Marc B Nov 20 '14 at 21:41
  • 1
    It is contiguous. But be careful with the construct you are using. Variable length arrays (VLA) may quickly explote your stack, if `nodo` gets too large. – Jens Gustedt Nov 20 '14 at 21:55
  • C11 compilers are also [not required](http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29#Optional_features) to support VLAs at all. They are required under C99, which IMHO is somewhat weird. The upshot is that VLA code might theoretically fail to compile in the future, depending on your choice of compiler. – Kevin Nov 20 '14 at 22:02
  • http://stackoverflow.com/questions/2832970/does-c99-guarantee-that-arrays-are-contiguous – Lee Daniel Crocker Nov 20 '14 at 22:16

1 Answers1

5

Yes, it is contiguos. Basically it is just syntactic sugar for distancias[nodo*nodo].

Philipp Murry
  • 1,660
  • 9
  • 13