Arrays, Structs, Pointers, and unexpected crashes

This is a discussion on Arrays, Structs, Pointers, and unexpected crashes within the C Programming forums, part of the General Programming Boards category; (GAH. Why? Why did it all have to disappear??? sorry for my frustration. After typing out my problem for about ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    Arrays, Structs, Pointers, and unexpected crashes

    (GAH. Why? Why did it all have to disappear??? sorry for my frustration. After typing out my problem for about half an hour I was required to log back in, losing all of my work.)

    Hello all again,

    Because I don't want to type everything I said again, I'll just shorten it.

    I have an array of a struct (named CAR). this array is named "cars" (its inside a struct named park)
    Code:
    typedef struct park
    {
    	CAR cars[NUM_CARS];
    } Park;
    I could do this to set a variable in one of the cars:
    Code:
    p->cars[i].variable=something; // This would work
    But I needed a second array named dock. This array should have a link to certain CARs in the "cars" array. I tried this code.
    Code:
    p->cars[i].variable=something;
    p->dock[j]=p->cars[i];
    p->cars[i].variable=something_different;  // I want this to also affect p->dock[j]
    unfortunately, changing something in "cars" didn't change the same CAR in "dock." When I set one equal to the other, it copied instead of making a link.

    I realized my mistake and needed to have an array of pointers instead of an array of CARs. My new code:
    Code:
    typedef struct park
    {
    	CAR* cars[NUM_CARS];
    	CAR* dock[NUM_CARS];
    } Park;
    now I can copy cars correctly (by copying their address), but I can no longer set variables:
    Code:
    p->dock[i]=p->cars[i];  // this now works.
    p->dock[i].variable = something;  // this gives me a syntax error
    p->dock[i]->variable = something;  // this crashes the program upon executing
    So what did I do wrong and how do I fix it?

    Thanks for listening,
    Kairos

    P.S. I kinda wrote this in a hurry and not as well since everything died. If this doesn't make sense please tell me and I will clear it up for you.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    How about
    Code:
    typedef struct park
    {
    	CAR cars[NUM_CARS];
    	CAR* dock[NUM_CARS];
    } Park;
    "cars" should still be "real" cars, and dock can be pointers.

    Code:
    p->dock[i] = &(p->cars[i]);
    p->dock[i]->variable = something;
    [edit]
    The problem with your "both pointers" solution is that you have no "real" cars anywhere. If you want to use that approach, you would have to use a loop to allocate memory for the cars on the heap (using malloc() for example).

    The reason your
    Code:
    p->dock[i]->variable = something;  // this crashes the program upon executing
    crashes is that, while syntactically correct, you are trying to write to cars that don't exist. The pointers aren't initialized. They point to random places in memory (your program's addressing space actually), and you can't just write to anywhere you want .
    [/edit]
    Last edited by cyberfish; 04-13-2009 at 12:28 AM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    Thanks

    Wow. Thanks so much. Fast post too. I feel stupid for not catching that. I kinda facepalmed when you said that...

    Well you saved me a night of pulling my hair out. Thanks again. Now my program will work flawlessly (I hope).

    Thanks,
    Kairos

Popular pages Recent additions subscribe to a feed

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