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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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();
}
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
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?



Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.
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();

}
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.

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();

}
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;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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  );
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;
}

grg99Commented:
oH!  of course!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.