Originally Posted by
laserlight
Why do you say that "there is no point in trying to do this in C"?
Because it's true And there's a reason for it (it should never be necessary).
Originally Posted by
avi2886
But my program actually uses 2D arrays. In case its relevant.
Oh I'm afraid so. You are only making life harder for yourself. I know you don't want to use the return value but consider this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float **testfunc(int x, int y) {
float **ray=malloc(y*sizeof(float*)); /* array of float pointers */
int i,ii;
for (i=0; i<x; i++) {
ray[i]=malloc(y*sizeof(float)); /* each pointer to an array of floats */
for (ii=0; ii<y; ii++) ray[i][ii]=i+(ii*0.1);;
}
return ray;
}
int main() { /* 4x6 float array */
int i,ii;
float **this=testfunc(4,6);
for (i=0; i<4; i++) {
for (ii=0; ii<6; ii++) printf("%.1f ",this[i][ii]);
printf("\n");
free(this[i]); /* malloced seperately... */
}
free(this); /* ...free seperately */
return 0;
}
Output:
0.0 0.1 0.2 0.3 0.4 0.5
1.0 1.1 1.2 1.3 1.4 1.5
2.0 2.1 2.2 2.3 2.4 2.5
3.0 3.1 3.2 3.3 3.4 3.5
I didn't check the malloc calls as laserlight rightly does.