Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereferencing a pointer in Objective-C

Can anyone explain me why we aren't dereferencing a pointer "now" to an NSDate instance, when we actually want to get the data from this instance, not an address.

NSDate *now = [NSDate date];
NSLog(@"The date is %@", now);

The fact why i'm confused is that the previous example of NSLog usage in Aaron Hillegass "Objective-C Programming" book was:

NSDate *now = [NSDate date];
NSLog(@"The new date lives at %p", now);

This code is clear. We want address, we get it. But how do we get the actual date just by changing the specifier when we continue working with the pointer?

like image 745
Vladislav Zakatov Avatar asked Jan 02 '12 21:01

Vladislav Zakatov


People also ask

What is dereferencing of pointer in C?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.

How do you dereference a pointer object?

The . * operator is used to dereference pointers to class members. The first operand must be of class type. If the type of the first operand is class type T , or is a class that has been derived from class type T , the second operand must be a pointer to a member of a class type T .

Does Objective C use pointers?

Objective-C allows you to have pointer on a pointer and so on. Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function.

What is the purpose of dereferencing a pointer data type?

Dereferencing means to get at the data contained at the memory location the pointer is pointing at. This means that one will know what type of data will be read from the memory.


1 Answers

The "actual date" is a concept. The "data" is a series of bytes. They are not the same thing.

If you were using good old fashioned C-style strings and printf, you would write this:

char *name = "Zakatov";
printf("My name is %s", name);

This is because the printf function needs the address of the thing to work with it. You're not passing "the actual data" but the address because it is way more efficient to pass the address around. Passing something to a function means copying its value to the stack area of memory, and it's way faster to copy an address (32 or 64 bits) than to copy a whole string (several bytes, or maybe KB or MB).

So in Objective-C we deal with pointers to objects all the time, because pointers are the easiest way to refer to them. The only thing that needs to dereference the pointers (look at the bytes) is the runtime system, for example when it is translating message selectors into function addresses to execute them. Your code just treats the pointer as a pointer, and passes it around without worrying about the precise layout of the data at the other end (the bytes).

like image 58
benzado Avatar answered Oct 28 '22 01:10

benzado