Thread: structure addresses...arg!

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    structure addresses...arg!

    howdy everyone,
    here's the question...I have a function that returns a pointer to a structure...now I'm trying to send that info into my main function by assigning it to a variable. So in main I have
    Code:
    time trip = sum(&day1, &day2);
    Which doesn't work assumably because I can't assign a value of type pointer to struct to a fundamental type (correct?)...so I convert it to:
    Code:
    time* trip = sum(&day1, &day2);
    Which also doesnt work, I'm assuming because if trip is a pointer I can't initialize it to the value of a function? I'm not sure why it doesn't work actually...and I was wondering if anyone had an answer for me...and also I was curious if it doesn't work for the second reason how would I make the variable trip accept a pointed to value?
    Phew...thanks yet again -Chap

    PS - If I'm not being clear at all...feel free to insult me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    time trip = *sum(&day1, &day2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    ok...I found out what was wrong...I created this new pointer to accept the function return:
    Code:
    time* p_trip;
    p_trip = sum(&day1, &day2);
    show_time(p_trip);
    Quick question...does this look like a reasonable solution? Obviously it works so it's A soluation...I was wondering if this isn't optimal...I'd hate to develop bad habits so early in my programming...-Chap

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    interesting code Salem...now what would the * before the sum function do to that function call? I'm very curious...thanks for the reply by the way...-Chap

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Same as it does with
    myint = *myintptr;

    You have something which is a pointer, the * dereferences that pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    oh wow...i didnt know you could dereference a function call...that's awesome information salem...thanks!

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Don't think of it as dereferencing a function call. Think of it as derefrencing the pointer that was returned by the function call. The whole line is like a 2 step processes.
    1.) Call function. Function returns something.
    2.) Assign what the function returned to the left value. You aren't assigning the "function", your assigning the thing returned by the function.

    Hope that helps

    PK

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    that's makes a lot more sense thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM