Dear all,

I have to use a funny structure in my code which has a 2D array and allocate it in the program. I do not know how to use malloc for that. This is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define DIM                                                         (int)50

struct Results{



int main(){

  int i, j;

  struct Results results[DIM][DIM];

  /* now I want to use malloc to get space for DV*/

  for(i = 0; i < DIM; i++){
    for(j = 0; j < DIM; j++){

     /* it should be something like this */
      results[i][j].DV = (double(*)[DIM]) 
    malloc(DIM * sizeof(results[i][j].(*DV) ));


  return 0;


But I get the following error during compilation:
te.c:32: error: expected identifier before ‘(’ token
te.c:32: error: incompatible types when assigning to type ‘double [4]’ from type ‘double ()[50]’

Could you tell me please how can I use malloc here?
Thanks in advance for your help.

Edited by Reverend Jim: Fixed formatting

6 Years
Discussion Span
Last Post by Salem

8 posts, no code tags.
You've got two more attempts to get your act together.

Take it as a warning, but as a honor as well that you did not get completely ignored, for purposely ignoring the code of conduct of this forum.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.