[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parameter passing by pointer

Posted on 2006-04-05
11
Medium Priority
?
243 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 );

}
0
Comment
Question by:thanesh
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 14

Expert Comment

by:wayside
ID: 16386477
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
 
LVL 86

Expert Comment

by:jkr
ID: 16386507
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
 
LVL 22

Expert Comment

by:grg99
ID: 16386958
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16388802
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
 
LVL 1

Author Comment

by:thanesh
ID: 16391918
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
 
LVL 22

Expert Comment

by:grg99
ID: 16392206
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
 
LVL 1

Author Comment

by:thanesh
ID: 16392302
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
 
LVL 14

Accepted Solution

by:
wayside earned 600 total points
ID: 16392367
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
 
LVL 22

Expert Comment

by:grg99
ID: 16392392
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 16392408
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
 
LVL 22

Expert Comment

by:grg99
ID: 16392828
oH!  of course!

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question