P
Paminu
Why make an array of pointers to structs, when it is possible to just make
an array of structs?
I have this struct:
struct test {
int a;
int b;
};
I have then made an array:
struct test testarray[5];
I would then like to shift all the elements one index to the right and
afterwards insert a new pkt struct at index 0.
something like this:
I would like to insert 3 different test structs with "b": 10, 20, and 30.
When I iterate through the testarray and make a printout of testarray.b,
I should see something like this:
10, 0, 0, 0, 0
20, 10, 0, 0, 0
30, 20, 10, 0, 0
For this to work I have made this function:
void insertIntoBuffer(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}
testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray.b);
}
printf("\n");
}
It seems to work fine, so I can't seem to see any reason to mess around with
an array of pointers.
Are there anything that I cannot do with this simple solution that would be
possible with an array of pointers to structs?
an array of structs?
I have this struct:
struct test {
int a;
int b;
};
I have then made an array:
struct test testarray[5];
I would then like to shift all the elements one index to the right and
afterwards insert a new pkt struct at index 0.
something like this:
I would like to insert 3 different test structs with "b": 10, 20, and 30.
When I iterate through the testarray and make a printout of testarray.b,
I should see something like this:
10, 0, 0, 0, 0
20, 10, 0, 0, 0
30, 20, 10, 0, 0
For this to work I have made this function:
void insertIntoBuffer(struct test tt)
{
int c;
for(c=3; c>=0; c--)
{
testarray[c+1] = testarray[c];
}
testarray[0] = tt;
int i;
for (i = 0; i < 5; i++ )
{
printf("Added b %d\n", testarray.b);
}
printf("\n");
}
It seems to work fine, so I can't seem to see any reason to mess around with
an array of pointers.
Are there anything that I cannot do with this simple solution that would be
possible with an array of pointers to structs?