Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 27

Ten More Things You Need to Know about the C Language

In This Chapter

Using arrays

Understanding strings

Dealing with structures

Growing frustrated over pointers

Messing with linked lists

Manipulating bits

Interacting with the command line

Doing disk access

Interacting with the operating system

Building big programs

Your C language journey is far from over. C has so much more ground to be covered that it just couldn’t all possibly fit into this one book, at least

not at the same easygoing pace.

To give you a hint of things to come, here are ten more important aspects of the C language — and programming in general — that you may want to con­ sider pursuing. (Note that these topics are all covered in this book’s compan­ ion volume, C All-in-One Desk Reference For Dummies, also published by Wiley.)

Arrays

An array is a multivariable. It allows you to store many different integers, floating-point values, characters, strings, or any variable type in a single unit. Suppose that the combination to the Big Safe at Fort Knox is

32, 17, 96

340 Part V: The Part of Tens

Those three numbers are integers, of course. But the three of them stored together form an array.

Arrays are declared just like other variables, though the variable name ends with a set of square brackets:

int combination[3];

In this example, the combination array is declared. It can hold three items, which is what the 3 in square brackets specifies.

Arrays can also be declared and assigned at the same time, as in

float temps[] = { 97.0, 98.2, 98.6, 99.1 };

This array is named temps and contains four floating-point values.

Each item inside the array can be referred to individually. The items are known as elements.

The first element in an array is numbered zero. This is important to remember. So element zero in the temps[] array is 97.0.

Values are assigned to arrays just as they are to regular variables. For example:

combination[0] = 32; combination[1] = 17; combination[2] = 96;

These three statements assign the values 32, 17, and 96 to the three ele­ ments in the combination[] array. (Note that the first element is zero.)

The only bummer about arrays in the C language is that their size is fixed. After you set, or dimension, an array to hold a specific number of elements, that number is fixed and cannot be changed, which will cause you endless frustration in the future. For now, consider yourself forewarned.

Strings

A string is nothing more than an array of characters. For example:

char myname[] = “Dan”;

This declaration creates a string variable named myname. The contents of that variable are Dan, or the letters D, a, and n. You can also write it in a more tra­ ditional array style:

char myname[] = { ‘D’, ‘a’, ‘n’ };

Chapter 27: Ten More Things You Need to Know about the C Language 341

Strings always end with the NULL character, ASCII code 0, which is also defined in STDIO.H as the word NULL or character code \0 (backslash zero). Here’s the long way to create the string myname:

myname[0] = ‘D’; myname[1] = ‘a’;

myname[2] = ‘n’; myname[3] = ‘\0’;

That final NULL character tells the compiler where the string ends. Remember that strings can contain characters, such as Enter and Tab. The C language uses character code 0, or NULL, to mark the end of text.

A string is nothing more than a character array.

All strings end with the NULL character.

This book shows you how to use scanf() and gets() to read in strings. And, you can use the %s placeholder in the printf() function to display string values.

Many, many functions in the C language manipulate strings.

One C language string function is strcmp(), which is used to compare strings. Remember that you cannot use == in an if statement to com­ pare strings, but you can use the strcmp() or similar functions.

Structures

The C language lets you combine variables into a single package called a structure. It’s similar to a record in a database in that the structure variable can be used to describe a number of different things all at once. It’s roughly the equivalent of a 3-by-5 card.

Structures are declared by using the struct keyword. It’s followed by the name of the structure and its contents:

struct sample

{

int a; char b;

}

This structure is named sample. It contains two variables: an integer named a and a character named b. Note that this command merely creates the structure — it doesn’t declare any variables. To do that, you need another line.

342 Part V: The Part of Tens

The following line declares a structure variable named s1. The structure it uses is of the type defined as sample:

struct sample s1;

Suppose that you’re writing a game and need some way to track the charac­ ters inside the game. Consider the following structure:

struct character

{

char[10] name; long score;

int strength; int x_pos; int y_pos;

}

This structure is named character. It contains variables that describe variable attributes of a character in the game: the character’s name, score, strength, and location on the game grid.

To define four characters used in the game, the following declarations are needed:

struct character g1; struct character g2;

struct character g3; struct character g4;

Or:

struct character g1, g2, g3, g4;

Items within the structure are referred to by using dot notation. Here’s how the name for character g1 are displayed:

printf(“Character 1 is %s\n”,g1.name);

Suppose that character g2 is decimated by a sword thrust:

g2.strength -= 10;

This statement subtracts 10 from the value of g2.strength, the strength integer in character g2’s structure.

Yes, structures are one way to do database work in the C language.

Obviously, there is quite a bit to this structure thing — much more than can be included in one section.