Thread: Array of structs

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Question Array of structs

    Hi! I have problems with an array of structs. This is an example of my problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int size = 0;
    
    struct a{
    	int x;
    };
    
    void add(struct a *as){
    	as = realloc(as, size++ * sizeof(struct a));
    	as[size - 1].x = size;
    
    	printf("%d\n", as[size -1].x);
    }
    
    int main(){
    	struct a *as = NULL;
    	add(as);
    	
    	printf("%d\n", as[size - 1].x);
    	
    
    return 0;
    }
    Segmetation fault is the error. Why?

  2. #2
    Registered User Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    many errors....
    i)after u made ur data type named 'a',why r u using struct always....
    remember its like others ..int,float....why complicate it....
    ii)how will main() know that u changed something in add()...if u keep add() as void

    here's mine..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int size=0;
    
    typedef struct
    {
    	int x;
    }a;
    
    a *add(a *as)
    {
    	as = (a*)malloc(++size * sizeof(a));
    	as[size - 1].x = size;
    
    	printf("%d\n", as[size -1].x);
    	return as;
    }
    
    int main()
    {
    	a *as;
    	as =add(as);
    	
    	printf("%d\n", as[size-1].x);
    	
    
    	return 0;
    }
    I don't use realloc....malloc rocks!!

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    the add(as); in the original post is results in the same thing as the same line in your code. In the original, he passes a pointer to the structure, so any changes to as within the function are reflected in as in main. So as = add(as); is redundant.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Tanuj_Tanmay View Post
    many errors....
    i)after u made ur data type named 'a',why r u using struct always....
    remember its like others ..int,float....why complicate it....
    No it's not. This isn't C++. You cannot omit the word struct if you have not created a new type with typedef.
    Quote Originally Posted by Tanuj_Tanmay View Post
    ii)how will main() know that u changed something in add()...if u keep add() as void
    Well, considering they're changing size, they could compare the value before and after the call. However, unless they use a pointer to a pointer, their pointer won't be keeping track of the newly allocated memory. Returning it as you have shown will get around that limitation as well.

    Your program is wrong for what they're trying to do though. They're trying to have the ability to reallocate the array bigger and bigger each time. The actual problem is that the size is being incremented AFTER it's being allocated. So what's the problem? What's zero * something?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bladactania View Post
    the add(as); in the original post is results in the same thing as the same line in your code. In the original, he passes a pointer to the structure, so any changes to as within the function are reflected in as in main. So as = add(as); is redundant.
    That's wrong. You cannot change what the pointer points at, unless you actually have the address of that pointer. A pointer will let you change the value of what is pointed at. Not what is pointed at.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    I dont want that the add function returns a struct pointer. This works, but .... How can i save a int in x and to have the same value int the main?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by paolor View Post
    I dont want that the add function returns a struct pointer. This works, but .... How can i save a int in x and to have the same value int the main?
    you could pass pointer to pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    pointre to pointer? Hwo?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    pass in add(&as), make the function take add(a **as), perhaps?

    --
    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.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Thanks matsp, but how? Could you edit my code with your idea. Please

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by paolor View Post
    Thanks matsp, but how? Could you edit my code with your idea. Please
    Why? you cannot edit your code yourself?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Yes I can do it, but I cannot what to do.
    Please, could you post the right code.
    Last edited by paolor; 04-23-2009 at 11:51 AM.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >but I cannot what to do.
    haha i like that. lol

    Well, people here won't write the code for you here. We can just hint you but not write the code or give the code.

    Here is a peice of hint
    Code:
    void add(struct a **as){  <-- This is how the function prototype should look like
    and in the main

    Code:
    struct a *as = NULL;
    add(&as);
    As you can see your sending the address of the pointer 'as' to the function. But since you are passing the pointer address and function 'add' should refereing to pointer to a pointer struct a.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM

Tags for this Thread