Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you NOT use the asterisk (*) when declaring a variable in Objective C

I have just started learning objective c and the asterisk is giving me some trouble. As I look through sample code, sometime it is used when declaring a variable and sometimes it is not. What are the "rules" for when it should be used. I thought it had something to do with the data type of the variable. (asterisk needed for object data types, not needed for simple data types like int) However, I have seen object data types such as CGPoint declared without the asterisk as well? Is there a definitive answer or does it have to do with how and what you use the variable for?

like image 588
Jason Avatar asked Jul 23 '09 15:07

Jason


People also ask

What does asterisk before variable mean in C?

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value. In the C programming language, the deference operator is denoted with an asterisk (*).

What is asterisk used for in C?

The asterisk symbol in C programming language is used as a pre-fix before a variable name to specify that the variable can store address reference of a memory location, i.e. asterix (variable name) makes the variable a pointer.

What does the star mean in C?

In computer programming, the dereference operator or indirection operator, sometimes denoted by " * " (i.e. an asterisk), is a unary operator (i.e. one with a single operand) found in C-like languages that include pointer variables.

What does the asterisk before a collection data type variable mean?

It's a part of the syntax for declaring a pointer (for example, int * a; declares a having type "pointer to int ")


2 Answers

You use a * when the variable type is a class.

An example may help.

NSNumber *number;
NSInteger integer;

The NSNumber variable type is a class while NSInteger is just another name for a normal C-type int. As you can see here, the compiler replaces every occurrence of NSInteger with int:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

Further, you cannot declare an instance of a class(an object), like NSNumber, without using a pointer(thus you use a *). The reason for this is that when you alloc an instance of a class as an object a memory address is returned. A pointer is a type of variable that specifically refers to a memory location. For example:

NSNumber *number = [NSNumber alloc];

Here number's numeric value would be a memory location like 0x19a30c0. You could operate on it by adding and subtracting, like an int. The key purpose of declaring it as a NSNumber pointer is so the compiler can help the coder verify that the class has certain methods or to access known properties.


One last example:

NSInteger integer = [NSNumber alloc];

What would the value of integer be? In our example, it would be 0x19a30c0. With this you could actually still access the newly allocated NSNumber object via [integer someMethod]. The compiler would give you a warning, though. More over:

integer += 4;

This would affect the numeric value 0x19a30c0 by adding 4 to it and making it 0x19a30c4.


Look at this Wikipedia article on C/C++ pointers for some more examples of how, when, and why to use an * operator.

like image 83
Brenden Avatar answered Sep 23 '22 18:09

Brenden


What are the "rules" for when it should be used.

You use the asterisk to declare a pointer.

For a Cocoa object, you're always declaring a pointer, so you always use an asterisk. You can't put the object itself into the variable; you always handle a pointer to the object.

For other things, it depends on whether the variable will contain the object (in the C sense) or a pointer to the object-somewhere-else. If the variable should contain the object, then you don't declare it with an asterisk, because you're not putting a pointer in it. If it should contain a pointer, then you do declare it with an asterisk.

You can even have a pointer to a pointer; as you might expect, this involves multiple asterisks. For example, NSRect ** is a pointer to a pointer to an NSRect (which is a structure, not a Cocoa object).

I thought it had something to do with the data type of the variable. (asterisk needed for object data types, not needed for simple data types like int)

Sort of. The asterisk is needed for Cocoa objects because you can only handle pointers to Cocoa objects, never the objects themselves. But the rules for declaration are no different for Cocoa objects; they are exactly the same. You use the asterisk when you want a pointer variable; you don't when you want a non-pointer variable.

The only exception, the only difference for Cocoa objects from the usual rules, is that you are not allowed to declare a variable holding the object itself. That's why you never see a variable holding a Cocoa object instead of a pointer to one: the compiler won't allow it.

However, I have seen object data types such as CGPoint declared without the asterisk as well?

CGPoint is a structure, not a Cocoa object. As such, you can declare a variable that holds a CGPoint and not a pointer to one somewhere else.

like image 37
Peter Hosey Avatar answered Sep 22 '22 18:09

Peter Hosey