Online Book Reader

Home Category

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

By Root 480 0
i;

for (i = 0; i < 12; i++) {

if (i % 3 == 0) {

continue;

}

printf("Checking i = %d\n", i);

if (i + 90 == i * i) {

break;

}

}

printf("The answer is %d.\n", i);

return 0;

}

Build and run it:

Checking i = 1

Checking i = 2

Checking i = 4

Checking i = 5

Checking i = 7

Checking i = 8

Checking i = 10

The answer is 10.

Figure 7.4 continue

The do-while loop


None of the cool kids use the do-while loop, but for completeness, here it is. The do-while loop doesn’t check the expression until it has executed the block. Thus, it ensures that the block is always executed at least once. If you rewrote the original exercise to use a do-while loop, it would look like this:

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

{

int i = 0;

do {

printf("%d. Aaron is Cool\n", i);

i++;

} while (i < 13);

return 0;

}

Notice the trailing semicolon. That’s because unlike the other loops, a do-while loop is actually one long statement:

do { something } while ( something else stays true );

Here’s a flow-chart of this do-while loop:

Figure 7.5 do-while loop

Challenge


Write a program that counts backward from 99 through 0 by 3, printing each number. However, if the number is divisible by 5, it should also print the words “Found one!”. Thus, the output should look something like this:

99

96

93

90

Found one!

87

...

0

Found one!

8

Addresses and Pointers


Your computer is, at its core, a processor (the Central Processing Unit or CPU) and a vast meadow of switches (the Random-Access memory or RAM) that can be turned on or off by the processor. We say that a switch holds one bit of information.You’ll often see 1 used to represent “on” and 0 used to represent “off.”

Eight of these switches make a byte of information. The processor can fetch the state of these switches, do operations on the bits, and store the result in another set of switches. For example, the processor might fetch a byte from here and another byte from there, add them together, and store the result in a byte way over someplace else.

Figure 8.1 Memory and the CPU

The memory is numbered, and we typically talk about the address of a particular byte of data. When people talk about a 32-bit CPU or a 64-bit CPU, they are usually talking about how big the address is. A 64-bit CPU can deal with much, much more memory than a 32-bit CPU.

Getting addresses


In Xcode, create a new project: a C Command Line Tool named Addresses.

The address of a variable is the location in memory where the value for that variable is stored. To get the variable’s address, you use the & operator:

#include

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

{

int i = 17;

printf("i stores its value at %p\n", &i);

return 0;

}

Notice the %p token. That’s the token you can replace with a memory address. Build and run the program. You’ll see something like:

i stores its value at 0xbffff738

although your computer may put i at a completely different address. Memory addresses are nearly always printed in hexadecimal format.

In a computer, everything is stored in memory, and thus everything has an address. For example, a function starts at some particular address. To get that address, you just use the function’s name:

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

{

int i = 17;

printf("i stores its value at %p\n", &i);

printf("this function starts at %p\n", main);

return 0;

}

Build and run the program.

Storing addresses in pointers


What if you wanted to store an address in a variable? You could stuff it into an unsigned integer that was the right size, but the compiler will help you catch your mistakes if you are more specific when you give that variable its type. For example, if you wanted a variable named ptr that holds the address where a float can be found, you would declare it like this:

float *ptr;

We say that ptr is a variable that is a pointer to a float. It doesn’t store the value of a float; it points to an address where a float may be stored.

Declare a new variable named addressOfI that is

Return Main Page Previous Page Next Page

®Online Book Reader