Hi, I'm a C-newbie and I would like to know if I am doing something
wrong in the code below. It is working, but I'm afraid it might not be
correct because I don't really understand everything of it. There are
lots of pointers and pointers to pointers which makes me confused.
First my typedef:
typedef struct
{
double re;
double im;
} cplx;
What I need to do first is to allocate memory to hold N pointers to
cplx. I do it with this statement (correct or not?):
cplx **data = (cplx **)malloc(sizeo f(cplx *) * N);
if(!data) { /* malloc failed */ ... }
Next I need to malloc space for each struct in the list and fill it
with data. This is what I do (correct or not?):
for(i = 0; i < N; i++)
{
data[i] = (cplx *)malloc(sizeof (cplx));
data[i]->re = i; /* fill with testdata */
data[i]->im = N - i - 1; /* fill with testdata */
}
Then I will call a rearrange-function to rearrange the _pointers_ and
_not_ the data in the array. The function looks like this:
void rearrange(cplx **data, int n_elements)
{
cplx *tmp;
int i;
/* reverse the data */
for(i = 0; i < (n_elements / 2); i++)
{
tmp = data[i];
data[i] = data[n_elements - i - 1];
data[n_elements - i - 1] = tmp;
}
}
I call this function from main() the following way (correct or not?):
rearrange(data, N);
And finally clean up the memory (correct or not?):
for(i = 0; i < N; i++)
free(data[i]);
free(**data);
Finally, is this the right approach if you got a large list of structs
to be rearranged? I guess it's much faster swapping pointers than
swapping the entire data in the structs?
wrong in the code below. It is working, but I'm afraid it might not be
correct because I don't really understand everything of it. There are
lots of pointers and pointers to pointers which makes me confused.
First my typedef:
typedef struct
{
double re;
double im;
} cplx;
What I need to do first is to allocate memory to hold N pointers to
cplx. I do it with this statement (correct or not?):
cplx **data = (cplx **)malloc(sizeo f(cplx *) * N);
if(!data) { /* malloc failed */ ... }
Next I need to malloc space for each struct in the list and fill it
with data. This is what I do (correct or not?):
for(i = 0; i < N; i++)
{
data[i] = (cplx *)malloc(sizeof (cplx));
data[i]->re = i; /* fill with testdata */
data[i]->im = N - i - 1; /* fill with testdata */
}
Then I will call a rearrange-function to rearrange the _pointers_ and
_not_ the data in the array. The function looks like this:
void rearrange(cplx **data, int n_elements)
{
cplx *tmp;
int i;
/* reverse the data */
for(i = 0; i < (n_elements / 2); i++)
{
tmp = data[i];
data[i] = data[n_elements - i - 1];
data[n_elements - i - 1] = tmp;
}
}
I call this function from main() the following way (correct or not?):
rearrange(data, N);
And finally clean up the memory (correct or not?):
for(i = 0; i < N; i++)
free(data[i]);
free(**data);
Finally, is this the right approach if you got a large list of structs
to be rearranged? I guess it's much faster swapping pointers than
swapping the entire data in the structs?
Comment