Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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 343

Pointers

Over in Chapter 1, I explain how the C programming language is a mid-level language, containing both high-level and low-level programming attributes. Well, welcome to the lowest of the low. Pointers are used in C to directly manipulate the computer’s memory — specifically, variables stored inside memory.

At first, this idea seems utterly useless. I mean, why do you need a pointer when you can change a variable’s value by using a function or an equal sign? Yet, pointers give the C language a certain muscle power unlike any other programming language.

The problem with pointers is that it takes about four or five times to under­ stand them, and then about six or seven programs showing how they can be used before you really get into the good “Oh, I get it now!” moments.

Pointers are declared by using the asterisk, which is confusing because the asterisk also means multiplication.

The following line declares an integer pointer, s:

int *s;

The ampersand is also used in Pointer Land, but its implementation is so bizarre that I dare not mention anything further.

The number-one foul-up when it comes to pointers: not assigning them before they’re used. All pointers must be declared, assigned, and then used. I mean, duh!

Linked Lists

Want to terrify any university sophomore? Mention linked lists and they will blanche with horror. Linked lists are the bane of the C language, almost as horrid as pointers. But they’re really nothing devious — just a strange con­ cept because in order to know about linked lists, you must know about point­ ers. That can be tough.

In the C language, linked lists combine the concept of an array with struc­ tures and pointers. In a way, a linked list is really like an array of structures. But, unlike with an array of structures, you can add and remove structures from the linked list easily.

And that’s all I really want to say about that!

344 Part V: The Part of Tens

Binary Operators

Along with the pointer, another low-level aspect of C is the binary operator. Binary operators work to manipulate the bits inside any C language variable except for a float or double. Table 27-1 lists the lot of them.

Table 27-1

Bitwise Operators

Operator

Function

&

AND

 

 

^

Exclusive OR (EOR or XOR)

 

 

|

Inclusive OR

 

 

~

One’s complement

 

 

<<

Shift bits left

 

 

>>

Shift bits right

 

 

The only place I have seen these types of operators used are when programs interact with the operating system or PC hardware. For example, if some port on the PC has its low bit set, you can use one or more of these operators to determine whether that’s true.

A special feature of the >> and << operators is that they perform superfast binary division and multiplication. For example:

x = y >> 1;

This function divides the value of y by 2, which is what happens when you shift the bits in the value of y one step to the left. It’s much faster than using this statement:

x = y/2;

To divide y by 4, you can use this function:

x = y >> 2;

Now, you have to think in binary to make this work, but it’s possible. Like­ wise, you can double a value by using the << operator. But save that one for another day.

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

Interacting with the Command Line

In Chapter 22, you may have read briefly about how the main() function returns a value to the operating system when the program quits. That’s one way that a program can communicate with the operating system. The other way is to read in options directly from the command line. For example:

grep pirntf *.c

This shell command searches for misspellings in your C language source code. The command has two command-line arguments: pirntf and *.c. These two strings of text are passed to the main() function as arguments as well, which the program can then evaluate and act on, just as arguments passed to any function.

The problem with introducing such a thing in this book is that you need to understand more about arrays and pointers to be able to deal with the information passed to the main() function. That too will have to wait for another day.

Disk Access

One of the reasons you have a computer is to store information and work on it later. The C language is equipped with a wide variety of functions to read and write information from and to the disk drives. You can save data to disk using C just as you can with any program that has a File Save command — though in C, it is you who writes the File Save command.

Interacting with the Operating System

The C language also lets you perform operating system functions. You can change directories, make new directories, delete files, rename files, and do a host of other handy tasks.

You can also have your programs run other programs — sometimes two at once! Or, your program can run operating system commands and examine the results.

Finally, you can have your program interact with the environment and exam­ ine the state of your computer. You can even run services or prowl out on the network. Just about anything the computer can do, you can add into your program and do it yourself.

346 Part V: The Part of Tens

Building Big Programs

There’s nothing wrong with having a 50,000-line source code file. True, it takes longer to compile, and searching for mistakes takes time. But I wouldn’t recommend it.

The best approach to writing large programs is to break things up into smaller modules. For example, one module may contain disk routines; another, initial­ ization; and another, routines to display things on the screen. This strategy not only makes debugging things easier, but —here’s a secret — if you make the modules well enough, you can also reuse them on other programming projects. That definitely saves time over the long haul.

In C, each source code file is compiled to produce an object code file. The object files are then linked with various libraries to produce the final exe­ cutable file. It works quite well. In fact, you can share variables across differ­ ent source code modules, call functions in other modules, and do lots of other cool stuff.

This book lacks the room to show an example, so I put it in the companion volume, C All-in-One Desk Reference For Dummies (Wiley). There, you can find an example of such a program, one that I hope will inspire you to go on to bigger and better programming projects in the C language.