Online Book Reader

Home Category

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

By Root 415 0
done. Wouldn’t it be great if you could declare that buffer as part of the frame (on the stack) so that it would be deallocated automatically when the function is done executing? You can. Change main.c:

import

float averageFloats(float *data, int dataCount)

{

float sum = 0.0;

for (int i = 0; i < dataCount; i++) {

sum = sum + data[i];

}

return sum / dataCount;

}

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

{

// Declares the array as part of the frame

float gradeBook[3];

gradeBook[0] = 60.2;

gradeBook[1] = 94.5;

gradeBook[2] = 81.1;

// Calculate the average

float average = averageFloats(gradeBook, 3);

// No need to free the array!

// Cleanup happens automatically when the function returns

printf("Average = %.2f\n", average);

return 0;

}

Build and run it.

The string literal made it easy to pack an array with characters. There are also array literals. Use one to initialize gradeBook:

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

{

float gradeBook[] = {60.2, 94.5, 81.1};

float average = averageFloats(gradeBook, 3);

printf("Average = %.2f", average);

return 0;

}

Build and run the program.

Notice that you didn’t need to specify the length of gradeBook as 3; the compiler figures that out from the array literal. You can use this type in many places where you might use *. For example, change the declaration of averageFloats() to do this:

float averageFloats(float data[], int dataCount)

{

float sum = 0.0;

for (int i = 0; i < dataCount; i++) {

sum = sum + data[i];

}

return sum / dataCount;

}

Build and run the program.

36

Command-Line Arguments


You know the arguments to main() that I’ve been carefully avoiding discussing?

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

{

...

Now you are ready to learn about them. argv is an array of C strings. argc tells you how many strings are in the array. What do these string represent? Command-line arguments.

The command-line tools that you’ve been creating can be run from Terminal. The Terminal app is just a pretty interface to what is called a shell. There are a few different shells with catchy names like csh, sh, zsh, and ksh, but nearly all Mac users use bash. When you run a program from bash, after you type in the program name, you can supply any number of arguments separated by whitespace. Those arguments are packed into argv before main() is called.

Truthfully, Cocoa and iOS programmers seldom use argv and argc. However, if you ever write a handy command-line tool, you will almost certainly need to know how to utilize them.

In Xcode, create a new C Command Line Tool project called Affirmation. Affirmation will take two arguments, a person’s name and a number n. When you run it, that person will be declared cool n times.

$ Affirmation Mikey 3

Mikey is cool.

Mikey is cool.

Mikey is cool.

Before we do that, change main() to just print out each of the arguments in argv:

#include

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

{

for (int i = 0; i < argc; i++) {

printf("arg %d = %s\n", i, argv[i]);

}

return 0;

}

If you are running this from bash, you could just type in the arguments on the command line.

$ Affirmation Aaron 4

However, to run a program with arguments in Xcode, you must first edit the scheme. Under the Product menu, choose Edit Scheme.... When the sheet appears, select Run Affirmation in the table view on the left. Then select the Arguments tab from the choices at the top of the sheet. Find the list entitled Arguments Passed On Launch and use the + button to add two items: a name and a number.

Figure 36.1 Adding arguments

Click OK to dismiss the sheet.

When you run the program, you’ll get a list of the strings in argv. The one that surprises most people is argv[0]:

arg 0 = /Users/aaron/Library/Developer/Xcode/DerivedData/

Affirmation-enkfqsgavfsproeggoxwbrmcowvn/Build/Products/Debug/Affirmation

arg 1 = Aaron

arg 2 = 4

argv[0] is the path to the executable file.

Figure 36.2 argv and argc in Affirmation

If your program takes

Return Main Page Previous Page Next Page

®Online Book Reader