Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [23]

By Root 458 0
*myPointer;

// Set myPointer to NULL for now, I'll store a pointer there

// later in the program

myPointer = NULL;

What is NULL? Remember that an address is just a number. NULL is zero. This is very handy in if statements:

float *myPointer;

...

// Has myPointer been set?

if (myPointer) {

// myPointer is not NULL

...do something with the data at myPointer...

} else {

// myPointer is NULL

}

Later, when we discuss pointers to objects, we will use nil instead of NULL. They are equivalent, but Objective-C programmers use nil to mean the address where no object lives.

Stylish pointer declarations


When you declare a pointer to float, it looks like this:

float *powerPtr;

Because the type is a pointer to a float, you may be tempted to write it like this:

float* powerPtr;

This is fine, and the compiler will let you do it. However, stylish programmers don’t.

Why? You can declare multiple variables in a single line. For example, if I wanted to declare variables x, y, and z, I could do it like this:

float x, y, z;

Each one is a float.

What do you think these are?

float* b, c;

Surprise! b is a pointer to a float, but c is just a float. If you want them both to be pointers, you must put a * in front of each one:

float *b, *c;

Putting the * directly next to the variable name makes this clearer.

Challenges


Write a program that shows you how much memory a float consumes.

On your Mac, a short is a 2-byte integer, and one bit is used to hold the sign (positive or negative). What is the smallest number it can store? What is the largest? An unsigned short only holds non-negative numbers. What is the largest number it can store?

9

Pass By Reference


There is a standard C function called modf(). You give modf() a double, and it calculates the integer part and the fraction part of the number. For example, if you give it 3.14, 3 is the integer part and 0.14 is the fractional part.

You, as the caller of modf() want both parts. However, a C function can only return one value. How can modf() give you both pieces of information?

When you call modf(), you will supply an address where it can stash one of the numbers. In particular, it will return the fractional part and copy the integer part to the address you supply. Create a new project: a C Command Line Tool named PBR.

Edit main.c:

#include

#include

int main(int argc, const char * argv[])

{

double pi = 3.14;

double integerPart;

double fractionPart;

// Pass the address of integerPart as an argument

fractionPart = modf(pi, &integerPart);

// Find the value stored in integerPart

printf("integerPart = %.0f, fractionPart = %.2f\n", integerPart, fractionPart);

return 0;

}

This is known as pass-by-reference. That is, you supply an address (also known as “a reference”), and the function puts the data there.

Figure 9.1 The stack as modf() returns

Here’s another way to think about pass-by-reference. Imagine that you give out assignments to spies. You might tell one, “I need photos of the finance minister with his girlfriend. I've left a short length of steel pipe at the foot of the angel statue in the park. When you get the photos, roll them up and leave them in the pipe. I'll pick them up Tuesday after lunch.” In the spy biz, this is known as a dead drop.

modf() works just like a dead drop. You are asking it to execute and telling it a location where the result can be placed so you can find it later. The only difference is that instead of a steel pipe, you are giving it a location in memory where the result can be placed.

Writing pass-by-reference functions


There are two popular ways to describe the location of a point in 2-dimensional space: Cartesian coordinates and polar coordinates. In Cartesian coordinates, (x, y) indicates that you should go to the right x and then up y. In polar coordinates, (theta, radius) indicates that you should turn to the left by theta radians and go forward radius.

Figure 9.2 Polar and Cartesian coordinates

What if you wanted

Return Main Page Previous Page Next Page

®Online Book Reader