problem printing out structure after loading

This is a discussion on problem printing out structure after loading within the C Programming forums, part of the General Programming Boards category; ohmygod thts abs great! u r a genius! but i have a Q. can i not pass a copy of ...

  1. #16
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    ohmygod thts abs great! u r a genius!
    but i have a Q. can i not pass a copy of aCar instead of the address? will it not then be solved?

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Passing structs around is not good practice, as they tend to be rather big (or can get to be so), and cause problems, so stick with passing addresses.

    >>cInv->pList[cInv->curSize] = (Car *)malloc(sizeof(Car));
    Here you malloc()'d memory to hold the structure. All you need do now is put the structure in it.
    >>cInv->pList[cInv->curSize] = aCar;
    Here you've goofed, because you're overwritten your pointer to your malloc()'d memory with another pointer. Now you can't free the memory you malloc()'d.

    So, to fix it:
    Change:
    >>cInv->pList[cInv->curSize] = aCar;
    To :
    >>*(cInv->pList[cInv->curSize]) = *aCar;
    I think the syntax is correct, it's late and I could be wrong!

    Basically you can copy structs by direct assignment.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    it works!!! thank u so much. i was trying to figure this out for the last 6 hrs. shows how dumb i am. :-) thanx again!

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

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Problem about Creating structure
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 06-05-2007, 08:33 PM
  3. Replies: 9
    Last Post: 05-21-2007, 01:10 AM
  4. Homework HELP - Problem Printing to file
    By brentshreve in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 03:11 AM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM

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