Parameter passing by pointer

Hi Experts, I am having some problem with the following simple program.
the structure member a is not changed after CheckOne is called.  Why it is not working.
The output is 5, instead of 100.

typedef struct{
  int a;
}my_struct

class MyClass
{

   CheckOne( my_struct * val );
   CheckTwo();
}

MyClass::CheckOne(my_struct * val )
{
   val->a = 100;
}

MyClass::CheckTwo()
{
  my_struct some;
  some.a = 5;
  CheckOne(&some);

  printf("int val = %d\n", some.a );

}
LVL 1
thaneshAsked:
Who is Participating?
 
waysideCommented:
The problem is in your ReadTimeFromFile() function.

The gmtime() function returns a struct tm *. Assigning it to the when pointer that is passed in means that you are changing a different structure when you assign 10 to the fields. That is why the original object is not changed.

Try this:

bool TimeThread::ReadTimeFromFile(tm *when)
{
     time_t ltime;
     time(&ltime);
     struct tm *tmp_when = gmtime(&ltime);
     
     when->tm_hour = tmp_when->tm_hour;
     when->tm_min = tmp_when->tm_min;
     when->tm_sec = tmp_when->tm_sec;
     
     return true;
}
0
 
waysideCommented:
I cleaned up your code into something compileable and runnable, and I got 100 as the output. Don't know why you got 5. What compiler are you using?

#include <stdio.h>

typedef struct{
  int a;
}my_struct;

class MyClass
{
  private:

   void CheckOne( my_struct * val );
   
  public:
   void CheckTwo();
};

void MyClass::CheckOne(my_struct * val )
{
   val->a = 100;
}

void MyClass::CheckTwo()
{
  my_struct some;
  some.a = 5;
  CheckOne(&some);

  printf("int val = %d\n", some.a );

}

void main() {
 
  MyClass obj;
  obj.CheckTwo();
}
0
 
jkrCommented:
Something seems to be goind wrong on your side - the following

#include <stdio.h>

typedef struct{
  int a;
}my_struct;

class MyClass
{
public:

   void CheckOne( my_struct * val );
   void CheckTwo();
};

void MyClass::CheckOne(my_struct * val )
{
   val->a = 100;
}

void MyClass::CheckTwo()
{
  my_struct some;
  some.a = 5;
  CheckOne(&some);

  printf("int val = %d\n", some.a );

}

void main () {

MyClass mc;

mc.CheckTwo();
}

correctly yields

int val = 100
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
grg99Commented:
the code looks okay.  Although it's mighty confusing to have a method that passes a local variable to another method, said variable not being a part of any object.

Did you intend to have my_struct be inside the object?



0
 
rajeev_devinCommented:
The posted code seems OK.
Can you post the complete code (as your code don't have main).
The problem lies in some other place.
0
 
thaneshAuthor Commented:
Here is an abstract of my code.  The inTime variable is not initialized.
Thanks.

#include <stdio.h>
#include <time.h>

class TimeThread{
public:
      bool ReadTimeFromFile( tm *when);
      bool CheckTimeToSync( );
      TimeThread();
      virtual ~TimeThread();
private:
      FILE *m_statsFileP;
};

TimeThread::TimeThread()
{
}

TimeThread::~TimeThread()
{
      
}

bool TimeThread::CheckTimeToSync()
{
      time_t curr_time;
      time( &curr_time );    
      tm  inTime;
      ReadTimeFromFile( &inTime);
//      ->>>>>>>>>>>>>>>>>>>>>>>>>>>>> inTime is not initialized here
      time_t readTime = mktime(&inTime);
      if(difftime(curr_time, readTime) < 0 )  {
            printf("diff less than zero\n");
      }
      else if( difftime(curr_time, readTime) == 0 )
      {
            printf("diff equal to zero\n");
      }
      else {
            printf("greater than zero\n");
      }
      return false;
}

bool TimeThread::ReadTimeFromFile(tm *when)
{
      time_t ltime;
      time(&ltime);
      when = gmtime(&ltime);
      
      when->tm_hour = 10;
      when->tm_min = 10;
      when->tm_sec = 10;
      
      return true;
}


void main()
{

      TimeThread th;
      th.CheckTimeToSync();

}
0
 
grg99Commented:
I would intialize the intime variable to something goofy, then print out irts value right after returning.  using printf.

If you use a debugger, sometimes optimization will trick the debugger into priting the wriong value.

0
 
thaneshAuthor Commented:
Yes, I was using debugger.  I added printf's as suggested and the result is same.  The output from the program now is.

In ReadTimeFromFile tm_hour = 10
In CheckTimeToSync tm_hour after init = -858993460

bool TimeThread::CheckTimeToSync()
{
      time_t curr_time;
      time( &curr_time );    
      tm  inTime;
      ReadTimeFromFile( &inTime);
      
      printf("In CheckTimeToSync tm_hour after init = %d\n", inTime.tm_hour );
      time_t readTime = mktime(&inTime);
      if(difftime(curr_time, readTime) < 0 )  {
            printf("diff less than zero\n");
      }
      else if( difftime(curr_time, readTime) == 0 )
      {
            printf("diff equal to zero\n");
      }
      else {
            printf("greater than zero\n");
      }
      return false;
}

bool TimeThread::ReadTimeFromFile(tm *when)
{
      time_t ltime;
      time(&ltime);
      when = gmtime(&ltime);
      
      when->tm_hour = 10;
      when->tm_min = 10;
      when->tm_sec = 10;
      
      printf("In ReadTimeFromFile tm_hour = %d\n", when->tm_hour );
      return true;
}


void main()
{

      TimeThread th;
      th.CheckTimeToSync();

}
0
 
grg99Commented:
okay, put these lines please and tell us what shows up:


   inTime.tm_hour = 11;    inTime.tm_min = 22; inTime.tm_sec = 33;

       printf("In CheckTimeToSyncr BEFORE  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );
      printf("In CheckTimeToSyncr BEFORE  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );
      printf("In CheckTimeToSyncr BEFORE  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );

    ReadTimeFromFile( &inTime);

       printf("In CheckTimeToSyncr AFTER  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );
     printf("In CheckTimeToSyncr AFTER  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );
     printf("In CheckTimeToSyncr AFTER  REadTimeFromFile h:%12d  m:%12d s:%12d\n", inTime.tm_hour, nTime.tm_min  , nTime.tm_sec  );
0
 
jkrCommented:
The problem is

bool TimeThread::ReadTimeFromFile(tm *when)
{
     time_t ltime;
     time(&ltime);
     when = gmtime(&ltime); // <----------------------
     
     when->tm_hour = 10;
     when->tm_min = 10;
     when->tm_sec = 10;
     
     return true;
}

'gmtime()' returns a pointer to a 'struct tm', so 'when' is only overwritten locally. Try

bool TimeThread::ReadTimeFromFile(tm *when)
{
     time_t ltime;
     time(&ltime);
     *when = *gmtime(&ltime);
     
     when->tm_hour = 10;
     when->tm_min = 10;
     when->tm_sec = 10;
     
     return true;
}

0
 
grg99Commented:
oH!  of course!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.