how to know dynamic size of struct

This is a discussion on how to know dynamic size of struct within the C Programming forums, part of the General Programming Boards category; Dear all, Suppose i have a program that is consisted of a struct. this struct will be used as an ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    18

    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;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Any which way you do this, you will need to track the number of elements you have allocated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and my point was mostly towards when using dynamic allocation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by nonoob View Post
    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 ?

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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).

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by master5001 View Post
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by unikgila View Post

    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.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is why incrementing by a constant factor is a lovely thing

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by master5001 View Post
    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

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I could. But to be perfectly frank, I am not going to.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is why I am being so harsh. To be honest this question gets asked with great regularity.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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);
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Replies: 16
    Last Post: 11-23-2007, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21