Online Book Reader

Home Category

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

By Root 447 0
members have been assigned values.

Figure 10.1 Frame after member assignments

Most of the time, you use a structure declaration over and over again. So it’s common to create a typedef for the structure type. A typedef defines an alias for a type declaration and allows us to use it more like the usual data types. Change main.c to create and use a typedef for struct Person:

#include

// Here is the declaration of the type Person

typedef struct {

float heightInMeters;

int weightInKilos;

} Person;

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

{

Person person;

person.weightInKilos = 96;

person.heightInMeters = 1.8;

printf("person weighs %i kilograms\n", person.weightInKilos);

printf("person is %.2f meters tall\n", person.heightInMeters);

return 0;

}

Now that you’ve created a typedef, you can pass a Person structure to another function. Add a function named bodyMassIndex() that accepts a Person as a parameter and calculates BMI. Then update main() to call it:

#include

typedef struct _Person {

float heightInMeters;

int weightInKilos;

} Person;

float bodyMassIndex(Person p)

{

return p.weightInKilos / (p.heightInMeters * p.heightInMeters);

}

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

{

Person person;

person.weightInKilos = 96;

person.heightInMeters = 1.8;

float bmi = bodyMassIndex(person);

printf("person has a BMI of %.2f\n", bmi);

return 0;

}

Challenge


The first structure I had to deal with as a programmer was struct tm, which the standard C library uses to hold time broken down into its components. The struct is defined:

struct tm {

int tm_sec; /* seconds after the minute [0-60] */

int tm_min; /* minutes after the hour [0-59] */

int tm_hour; /* hours since midnight [0-23] */

int tm_mday; /* day of the month [1-31] */

int tm_mon; /* months since January [0-11] */

int tm_year; /* years since 1900 */

int tm_wday; /* days since Sunday [0-6] */

int tm_yday; /* days since January 1 [0-365] */

int tm_isdst; /* Daylight Savings Time flag */

long tm_gmtoff; /* offset from CUT in seconds */

char *tm_zone; /* timezone abbreviation */

};

The function time() returns the number of seconds since the first moment of 1970 in Greenwich, England. localtime_r() can read that duration and pack a struct tm with the appropriate values. (It actually takes the address of the number of seconds since 1970 and the address of an struct tm.) Thus, getting the current time as a struct tm looks like this:

long secondsSince1970 = time(NULL);

printf("It has been %ld seconds since 1970\n", secondsSince1970);

struct tm now;

localtime_r(&secondsSince1970, &now);

printf("The time is %d:%d:%d\n", now.tm_hour, now.tm_min, now.tm_sec);

The challenge is to write a program that will tell you what the date (4-30-2015 format is fine) will be in 4 million seconds.

(One hint: tm_mon = 0 means January, so be sure to add 1. Also, include the header at the start of your program.)

11

The Heap


So far, your programs have only used memory that has been in frames on the stack. This memory is automatically allocated when the function starts and automatically destroyed when the function ends. (In fact, local variables are often called automatic variables because of this convenient behavior.)

Sometimes, however, you need to explicitly claim a long line of bytes of memory and use it in many functions. For example, you might read a file of text into memory and then call a function that would count all the vowels in that memory. Typically, once you were finished with the text, you would let the program know you were all done so the program could reuse that memory for something else.

Programmers often use the word buffer to mean a long line of bytes of memory. (This explains the term “buffering” to describe the wait for YouTube to send you enough bytes for that cat video to get started.)

You claim a buffer of memory using the function malloc(). The buffer comes from a region of memory known as the heap, which is separate from the stack. When you’re done using

Return Main Page Previous Page Next Page

®Online Book Reader