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 NUMBER_DECISION_VARIABLES (int)4
#define DIM                                                         (int)50

struct Results{

  double *DV[NUMBER_DECISION_VARIABLES];

};


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.

Recommended Answers

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

Jump to Post

All 3 Replies

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

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.

Better make it 1 post...
http://www.daniweb.com/forums/thread267007.html

"Last edited by peter_budo; 1 Hour Ago at 16:02. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) "

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.