S
Serpent
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html
I was using something slightly different from the C-FAQ and I was
wondering if it was legal.
Say I want a two-dimensional array, like this:
int x[2][3];
but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.
int i = x[0][1];
I also want it to be one contiguous piece of memory, to avoid overhead
and complications.
Is this legal?
int (*x)[3] = (int (*)[3]) malloc(sizeof(int) * 2 * 3);
x[0][0] = 1;
x[0][1] = ...;
x[1][1] = ...;
I don't see anything in the standard that wouldn't allow this. It is
also cleaner than what the C-FAQ mentions, which is:
int (*x)[2][3] = (int (*)[2][3]) malloc(sizeof(int) * 2 * 3);
(*x)[0][0] = 1;
(*x)[0][1] = ...;
(*x)[1][1] = ...;
which I want to avoid.
Thanks for any input. If this is illegal, a pointer into the standard
would be appreciated.
if this is legal, I will look into updating the C-FAQ.
-Frank
I was using something slightly different from the C-FAQ and I was
wondering if it was legal.
Say I want a two-dimensional array, like this:
int x[2][3];
but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.
int i = x[0][1];
I also want it to be one contiguous piece of memory, to avoid overhead
and complications.
Is this legal?
int (*x)[3] = (int (*)[3]) malloc(sizeof(int) * 2 * 3);
x[0][0] = 1;
x[0][1] = ...;
x[1][1] = ...;
I don't see anything in the standard that wouldn't allow this. It is
also cleaner than what the C-FAQ mentions, which is:
int (*x)[2][3] = (int (*)[2][3]) malloc(sizeof(int) * 2 * 3);
(*x)[0][0] = 1;
(*x)[0][1] = ...;
(*x)[1][1] = ...;
which I want to avoid.
Thanks for any input. If this is illegal, a pointer into the standard
would be appreciated.
if this is legal, I will look into updating the C-FAQ.
-Frank