how to know dynamic size of struct

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-20-2008
unikgila
how to know dynamic size of struct
Dear all,

Suppose i have a program that is consisted of a struct. this struct will be used as an array.
Code:

```typedef struct {         char name[30];         int salary; } person;```
How do i define an flexible array for this struc ? i don't know how many arrays which i need yet.

and how can i calculate the sum of all person salary ?

I wrote the simple code, but i think not solve my problem
Code:

```#include <stdio.h> typedef struct {         char name[30];         int salary; } person; int sumStructs(person*, int); main(void) {         person a[10], *pa;                  /* I still put non flexible array*/         pa = a;         int i = 0;         for(i=0; i< 10; i++)         {                 a[i].i = i+1;         }                 int d = sizeof(a)/sizeof(person);                 int c = sumStructs(pa, d);         printf("sum i adalah %d \n",c); } int sumStructs(person*pa, int d) {         int sum = 0, j= 0;         while(j<d)         {                 sum += (*pa).i;                 pa++;                 j++;         }         return sum; }```
• 10-20-2008
master5001
std::vector.....

Seriously though, just keep track of how big your array needs to be, and over-allocate a buffer. And use realloc() where necessary.
• 10-20-2008
nonoob
Code:

```person *a; a = malloc(sizeof(person) * number_of_elements);```
Then you'd access the array like this:
Code:

`a[i].name`
or
Code:

`a[i].salary`
• 10-20-2008
matsp
Any which way you do this, you will need to track the number of elements you have allocated.

--
Mats
• 10-20-2008
nonoob
Yeah, sorry. I was gonna mention that. But I saw that
Code:

`int sumStructs(person*pa, int d)`
already has provisions for passing the number of elements.
• 10-20-2008
matsp
Yes, and my point was mostly towards when using dynamic allocation.

--
Mats
• 10-20-2008
unikgila
Quote:

Originally Posted by nonoob
Yeah, sorry. I was gonna mention that. But I saw that
Code:

`int sumStructs(person*pa, int d)`
already has provisions for passing the number of elements.

Hi Nonoob,

You mean, i don't need to define variable a[10] instead of just put *a ?
I follow your idea, but i got the 0 result for sumStructs function. I modify my code like this:

Code:

```#include <stdio.h> typedef struct {         char name[30];         int salary; } person; int sumStructs(person*, int); main(void) {         person *pa;              pa = malloc(sizeof(student) * 100);                            int i = 0;         for(i=0; i< 10; i++)         {                 pa[i].i = i+1;                                  //put the salary value for each person         }                 int d = sizeof(pa)/sizeof(person);                 int c = sumStructs(pa, d);         printf("sum i is %d \n",c); } int sumStructs(person*pa, int d) {         int sum = 0, j= 0;         while(j<d)         {                 sum += (*pa).i;                 pa++;                 j++;         }         return sum; }```
and result is :
Code:

```\$ ./a.exe sum i is 0```
How do i get the number of element without making the array variable ?
• 10-20-2008
master5001
No...............

sizeof(pa) == 4 (or more)
sizeof(person) == 8 or whatever

So your parameter will evaluate to 0 (since .029 is not an integer value--I remember getting into a big argument about this fact once before).
• 10-20-2008
unikgila
Quote:

Originally Posted by master5001
No...............

sizeof(pa) == 4 (or more)
sizeof(person) == 8 or whatever

So your parameter will evaluate to 0 (since .029 is not an integer value--I remember getting into a big argument about this fact once before).

You are right, however if i still define
struct a[10];
sizeof(a) == 360

I think my first question still was not answered, because the memory already prepare for 10 variables a
• 10-20-2008
tabstop
Quote:

Originally Posted by unikgila

How do i get the number of element without making the array variable ?

You don't. You did the malloc -- did you forget between now and then how many elements you malloced? It's your responsibility to remember, not the compiler's.
• 10-20-2008
master5001
This is why incrementing by a constant factor is a lovely thing :)
• 10-20-2008
unikgila
Quote:

Originally Posted by master5001
This is why incrementing by a constant factor is a lovely thing :)

Sorry for not understand what you explain :( . If possible, i hope you can give me the sample code or repair the code that i have wrote above. Thanks
• 10-20-2008
master5001
I could. But to be perfectly frank, I am not going to.
• 10-20-2008
master5001
Here is why I am being so harsh. To be honest this question gets asked with great regularity.
• 10-20-2008
tabstop
Code:

```int main(void) {         person *pa;         int number_I_must_remember_because_its_the_size_of_my_array = 100;              pa = malloc(sizeof(student) * number_I_must_remember_because_its_the_size_of_my_array);                             int i = 0;         for(i=0; i< number_I_must_remember_because_its_the_size_of_my_array; i++)         {                 pa[i].i = i+1;                                  //put the salary value for each person         }                 int c = sumStructs(pa, number_I_must_remember_because_its_the_size_of_my_array);         printf("sum i is %d \n",c); }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last