We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Parameter passing by pointer

thanesh
thanesh asked
on
Medium Priority
260 Views
Last Modified: 2010-04-01
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 );

}
Comment
Watch Question

Commented:
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();
}
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Commented:
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?



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.

Author

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

}

Commented:
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.

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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  );
jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
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;
}

Commented:
oH!  of course!

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.