Hello

Hello i have struct dynamic array (matrixCov) like this

*array = (struct matrixCov*)malloc(sizeof(struct matrixCov) * counter); 

now i have another struct Link list with element Data type -struct matrixCov (struct matrixCov * data)
now when i type this line

for (k=0;k<conter;i++){
(*pointer)->data =array[k];}

when k=0 copied well but from k=1 its doesnt work
what can be the problem?

Edited 7 Months Ago by humble55

yes By mistake but still i cant understand why its dosent equal to same adress

 for (k=0;k<conter;i++){
    (*pointer)->data =array[k];}

Even if you change the loop counter tok++ I don't understand what this loop is trying to do. I don't understand why this is a loop at all. Let's say that conter is 5. Your loop is as follows:

(*pointer)->data = array[0];
(*pointer)->data = array[1];
(*pointer)->data = array[2];
(*pointer)->data = array[3];
(*pointer)->data = array[4];

I see nowhere that (*pointer) is changed in this loop, so you are writing to the same address five times. You are continually overwriting the previous write, so only the last iteration has an effect, so the above is equivalent to the single line below.

(*pointer)->data = array[4];

In other words, what is the point of the loop if the value of (*pointer) does not change inside the loop?

I'm also seeing what looks like a problem here:

*array = (struct matrixCov*)malloc(sizeof(struct matrixCov) * counter);

I'm assuming array is declared as:

struct matrixCov* array;

If so, wouldn't this be the appropriate malloc statement (no asterisk in front of array)?

array = (struct matrixCov*)malloc(sizeof(struct matrixCov) * counter);

I can't really make sense of the types of these variables. I'm assuming from the malloc statement that array is an array of matrixCov elements. I don't know what pointer is. Regardless of what it is, it seems like you would want something like this:

pointer->data =array[k].data;

rather than this:

(*pointer)->data = array[k];

But again, I'd have to see the declarations since I don't no what type pointer is and what you're trying to do.

This question has already been answered. Start a new discussion instead.