1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Rep Power

    How do I get a structure member out of a pointer?

    Hi there,
    Being a novice in C I have lot of question here is one of them and it is in the bugbear of C beginners that is Pointers:

    I have the function signature:

    float timer(struct timespec* ts1)
    struct tm startT;
    startT = *(localtime ((time_t *) &(start.tv_sec)));



    When I compile this I get an error because the method localtime in Time.h expects a timespec structure itself and NOT A POINTER to the timespec structure. Which brings me to the simple question how can I get access to the variable I am behind (start.tv_sec, which is a member of the structure) using the pointer that I have. I want to avoid passing in the structure itself.

    Your input is appreciated.

  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    in the perfect world
    Rep Power
    try something like

    tm	*pTime=NULL;//pointer to a tm struct
    time_t	SysTime;//an actual instance of the struc
    //get the time
    time(&SysTime);// the & sends in a pointer to our struct
    //convert to local
    //error check
    if(pTime==NULL)//still NULL
          //handle error
    //to look at an element of our struct pointer use the -> not .
    Last edited by TechNoFear; September 12th, 2003 at 02:07 AM.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo