Dear all,

I have to use a funny structure in my code which has a 2D array and allocate it later in the program. I do it but I get error which is written below. I have urgently to finish this part of the code. Would be agreat help if you can help me. 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.

I am not sure that you necessarily want to use malloc or #define but there are a couple of obvious errors in your code

you have a size mismatch
DV is 4 double *
where as you try to set it to a random pointer of size 50;
What you should use is just a double *

like:

double * pd = new double[DIM];
//use the 
result[i][j].DV[0] = pd;

you should then use delele[] later on the pd;

I think that there are other issues about DV but basically a pointer
is it meant double * DV; ?

pointer can just point to the first element of an array and then store the size elsewhere. What is the struct supposed to be doing?

Dear all,
results[j].DV = (double(*)[DIM])

Dear all,

I have to use a funny structure in my code which has a 2D array and allocate it later in the program. I do it but I get error which is written below. I have urgently to finish this part of the code. Would be agreat help if you can help me. 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.

Line 32 is a bracket, so the error message doesn't make sense. Make sure that the error message you list matches the code you post. Otherwise we have no idea where to look.

In your case, you are missing a semicolon on line 29 at the very least.

I'm not sure you need to use malloc at all. You use malloc (or actually "new" instead of "malloc" usually in C++) when you don't know the array size ahead of time. You already know what NUMBER_OF_DECISION_VARIABLES is (4), so just delete the asterisk in line 12 and you have an array of 4 without any malloc.

I don't understand the need for a typecast in lines 7 and 8. I would delete the "(int)".

Regarding how to use malloc, if you WERE to use it, it's normally something like this:

int numElementsInArray;
int* array;

cout << "Enter number of elements: ";
cin >> numElementsInArray;

array = (int*) malloc (numElementsInArray * sizeof (int));

// do stuff

free (array);

But again, I don't think you need new or malloc here, and in C++, you usually use new/delete rather than malloc/free.

Be a part of the DaniWeb community

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