Thread: struct pointer problem (out of scope?)

  1. #1
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    struct pointer problem (out of scope?)

    Why doesn't this work

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void setTime(time_t *curTime,struct tm *retDate);
    
    int main(int argc, char *argv[])
    {
     time_t now = 0;
     struct tm *curDate;
    /* This would work
     now = time(NULL);
     curDate = localtime(&now);
    */
     printf("main()-Before:\ntime_t now: %x\tstruct tm *curDate: %x\n\n\n",&now,curDate);
     setTime(&now,curDate);
     printf("main()-After:\ntime_t now: %x\tstruct tm *curDate: %x\n\n\n",&now,curDate);
    
     printf("%d\n\n",curDate->tm_hour);
    
     system("PAUSE");
     return 0;
    }
    
    void setTime(time_t *curTime,struct tm *retDate)
    {
     printf("setTime()-Before:\ntime_t now: %x\tstruct tm *curDate: %x\n\n\n",curTime,retDate);
    
     *curTime = time(NULL);
     retDate  = localtime(curTime);
    
     printf("setTime()-After:\ntime_t now: %x\tstruct tm *curDate: %x\n\n\n",curTime,retDate);
    }
    I don't get why this doesn't work... i'm no newb to C either so it's really disturbing me. Is there some kind out scope error with localtime? but retDate is a pointer to curDate which is in scope. Compile it and you'll see that it has the right numbers while in setTime(), but when it goes back to main() it looses the correct memory area. Thanks to all who read this and/or help, I truely appriciate it.
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you have a pointer outside of a function, and you want to change what it points to inside a function, then you need to pass a pointer to that pointer.

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

  3. #3
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    So you mean it's not at all possible to assign a function's argumental pointer a pointer value and have it retain it out of the scope of that function? I get why saying "mainPointer = setMainPointerTo();" if setMainPointerTo() returned an area of memory would make it within the scope of main. So in other words you're saying that you can't change a pointer's memory location from within a function and have it maintain it outside that function... right?
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    No. Quzah just told you how to accomplish it. You need to pass a pointer to the pointer to do it. Look at this simplified version of the problem:
    Code:
    void changeit(int *ptr)
    {
      *ptr = 5;
    }
    
    int main(void)
    {
      int a = 3;
    
      changeit(&a);
      return 0;
    }
    If you just pass a to the function then the change is only local to the changeit() function, but by passing a pointer to a you can change it.

    Your problem is the same, but instead of changing the value of an int, you're trying to change the value of a pointer. So if you need to pass a pointer to an int to change that int, then it stands to reason that you'd need to pass a pointer to a pointer to change that pointer.
    Last edited by itsme86; 05-17-2005 at 09:39 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Oh ok I get it now since localtime returns a pointer I need a pointer to the pointer, ok, thanks alot you guys I truley appriciate it! ^_^
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM