Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding pointers?

Tags:

c

objective-c

As the title suggests, I'm having trouble understanding exactly what a pointer is and why they're used. I've searched around a bit but still don't really understand. I'm working in Objective-C mainly, but from what I've read this is really more of a C topic (so I added both tags).

From what I understand, a variable with an asterisks in front points to an address in memory? I don't quite understand why you'd use a pointer to a value instead of just using the value itself.

For example:

NSString *stringVar = @"This is a test.";

When calling methods on this string, why is it a pointer instead of just using the string directly? Why wouldn't you use pointers to integers and other basic data types?

Somewhat off topic, but did I tag this correctly? As I was writing it I thought that it was more of a programming concept rather than something language specific but it does focus specifically on Objective-C so I tagged it with objective-c and c.

like image 873
Lee Avatar asked Dec 09 '22 01:12

Lee


2 Answers

I don't quite understand why you'd use a pointer to a value instead of just using the value itself.

You use a pointer when you want to refer to a specific instance of a value instead of a copy of that value. Say you want me to double some value. You've got two options:

  • You can tell me what the value is: "5": "Please double 5 for me." That's called passing by value. I can tell you that the answer is 10, but if you had 5 written down somewhere that 5 will still be there. Anyone else who refers to that paper will still see the 5.

  • You can tell me where the value is: "Please erase the number I've written down here and write twice that number in its place." That's called passing by reference. When I'm done, the original 5 is gone and there's a 10 in its place. Anyone else who refers to that paper will now see 10.

Pointers are used to refer to some piece of memory rather than copying some piece of memory. When you pass by reference, you pass a pointer to the memory that you're talking about.

When calling methods on this string, why is it a pointer instead of just using the string directly?

In Objective-C, we always use pointers to refer to objects. The technical reason for that is that objects are usually allocated dynamically in the heap, so in order to deal with one you need it's address. A more practical way to think about it is that an object, by definition, is a particular instance of some class. If you pass an object to some method, and that method modifies the object, then you'd expect the object you passed in to be changed afterward, and to do that we need to pass the object by reference rather than by value.

Why wouldn't you use pointers to integers and other basic data types?

Sometimes we do use pointers to integers and other basic data types. In general, though, we pass those types by value because it's faster. If I want to convey some small piece of data to you, it's faster for me to just give you the data directly than it is to tell you where you can find the information. If the data is large, though, the opposite is true: it's much faster for me to tell you that there's a dictionary in the living room than it is for me to recite the contents of the dictionary.

like image 64
Caleb Avatar answered Dec 25 '22 18:12

Caleb


I think maybe you have got a bit confused between the declaration of a pointer variable, and the use of a pointer.

Any data type with an asterisk after it is the address of a value of the data type.

So, in C, you could write:
char c;
and that means value of c is a single character. But
char *p;
is the address of a char.

The '*' after the type name, means the value of the variable is the address of a thing of that type.

Let's put a value into c:
c = 'H';

So
char *p;
means the value of p is the address of a character. p doesn't contain a character, it contains the address of a character.

The C operator & yields the address of a value, so
p = &c;
means put the address of the variable c into p. We say 'p points at c'.

Now here is the slightly odd part. The address of the first character in a string is also the address of the start of the string.

So for
char *p = "Hello World. I hope you are all who safe, sound, and healthy";
p contains the address of the 'H', and implicitly, because the characters are contiguous, p contains the address of the start of the string.

To get at the character at the start of the string, the 'H', use the 'get at the thing pointed to' operator, which is '*'.

So *p is 'H'

p = &c; if (*p == c) { ... is true ... }

When a function or method is called, to use the string of characters, the only the start address of the string (typically 4 or 8 bytes) need be handed to the function, and not the entire string. This is both efficient, and also means the function can act upon the string, and change it, which may be useful. It also means that the string can be shared.

like image 37
gbulmer Avatar answered Dec 25 '22 18:12

gbulmer