views:

1620

answers:

5

...without using typedef.

My boss claims he was once asked this in an interview, and when he gave his answer the interviewers told him he couldn't use typedefs because it was poor style.

Regardless, he likes to throw the question out at people just to see if they can get it right , usually at new programmer lunches. No one ever gets it right (especially without a pen and paper or computer handy). I want to be ready next time he tries to stump someone with it >:D

+2  A: 
void*** p2DArray = (void***)malloc( numAxis1 * sizeof( void** ) );

int count = 0;
while( count < numAxis1 )
{
    p2DArray[count] = (void**)malloc( numAxis2 * sizeof( void* ) );
    count++;
}

And there ya go. You can now access the array by going p2DArray[n][m] and get at the void* stored there.

Don't see why you'd need to use typedefs anyway ...

Edit: hahahaha or what avakar suggested ;)

Goz
In C, you should never cast the return of malloc().
unwind
@unwind: You should never cast the return of malloc? So you can't allocate anything else than void*, which is useless in itself?
OregonGhost
Nah char* pChar = malloc( 1 ); is valid code if i recall correctly. I don't seem to remember there being any harm in casting the return of malloc ... but I do use C++ more than C.
Goz
The harm is that if you forget to include the definition of malloc(), the cast will hide your error. Since it serves no real purpose...
caf
void pointers are implicitly converted to other object pointer types without the need for a cast. T *x = malloc(sizeof *x * num_elements);is idiomatic C. Prior to C89 malloc() returned char*, so a cast was necessary, but not that many of us are working on 20+ year old implementations.
John Bode
+5  A: 

void* array[m][n];

Will give you a 2D array of void pointers. I'm not sure what's so confusing about this, unless I misunderstood your question.

Falaina
A: 

Row major in C/C++, column major in Matlab/Fortran and for some unknown reason C#/native interop with multidimensional arrays.

int x = 4;
int y = 4;
void** data = malloc(x * y * sizeof(void*));
280Z28
A: 

The thing about typedefs and function pointers is that it easifies the mental strain of programmers.

I guess the problem comes from there, using void pointers would solve this, though you'd have to cast the FP whenever you're using it to get rid of all warnings.

Tobias Wärre
+1  A: 

2D array of pointers to what?

T *p[N][M];     // N-element array of M-element array of pointer to T
T (*p[N][M])(); // N-element array of M-element array of pointer to 
                // function returning T

If we're talking about pointers to 2D arrays, then things only get slightly more interesting:

T a[N][M];            // N-element array of M-element array of T
T (*p)[M] = a;        // Pointer to M-element array of T
T (**p2)[M] = &p;     // Pointer to pointer to M-element array of T
T (*p3)[N][M] = &a;   // Pointer to N-element array of M-element 
                      // array of T
T (**p4)[N][M] = &p3; // Pointer to pointer to N-element array of 
                      // M-element array of T

Edit: wait, I think I may be getting what you're after.

T *(*a[N])[M];        // a is an N-element array of pointer to M-element
                      // array of pointer to T

Edit: Now we get really silly:

T *(*(*a)[N])[M];     // a is a pointer to an N-element array of 
                      // pointer to M-element array of pointer to T

T *(*(*(*f)())[N])[M];  // f is a pointer to a function returning
                        // a pointer to an N-element array of pointer
                        // to M-element array of pointer to T

T *(*(*f[N])())[M];     // f is an N-element array of pointer to 
                        // function returning pointer to M-element 
                        // array of pointer to T

And for the pathologically insane:

T *(*(*(*(*(*f)())[N])())[M])(); // f is a pointer to a function 
                                 // returning a pointer to a N-element
                                 // array of pointer to function 
                                 // returning M-element array of
                                 // pointer to function returning
                                 // pointer to T

Typedefs are for wusses.

John Bode
"Typedefs are for wusses." LOOOOOL
Johannes Schaub - litb