Variable or pointer?


#1

Hi,

I’m reading chapter 12 about objects, especially this part :

    NSDate *now = [[NSDate alloc] init];
    NSLog(@"The new date lives at %p", now);
    NSLog(@"The date is %@", now);
    
    double seconds = [now timeIntervalSince1970];
    NSLog(@"It has been %f seconds since the start of 1970.", seconds);

    NSDate *later = [now dateByAddingTimeInterval:100000];
    NSLog(@"In 100.000 seconds, it will be %@.", later);

My question may be completely stupid, but here it is :
Would it be possible to define seconds as a pointer too ? (like *later)
Maybe not as an instance of NSDate class because it’s not really a date, but as an instance of NSObject for example ?
Because it’s also “filled” using an instance of a class and a date method.

Thanks for your comments !


#2

Yes you can, like this:

[code]double seconds;
double *pseconds = &seconds; // note the use of address of operator ‘&’

pseconds = [now timeIntervalSince1970]; // note the use of dereference operator '
NSLog(@“It has been %f seconds since the start of 1970.”, pseconds); // note the use of dereference operator '

[/code]

Or maybe like this:

void GetElapsedTime (double *pseconds);
...
double ET;
GetElapsedTime (&ET);

May I ask why you want to do that?


#3

thanks,

it was just a theoretical question, I was wondering why “seconds” was defined as a variable, while “later” was defined as a pointer.


#4

They are both variables: seconds is a plain variable for storing the value of seconds, later is a pointer variable for storing the address of an NSDate object.


#5

Aha, got it, thanks !!