Sams Teach Yourself C in 24 Hours (3 page)

BOOK: Sams Teach Yourself C in 24 Hours
6.21Mb size Format: txt, pdf, ePub
ads

Workshop ............................................................................................................171

Quiz ................................................................................................................171

Exercises ........................................................................................................172

Part III Pointers and Arrays

173

Hour 11 Understanding Pointers

175

What Is a Pointer?................................................................................................176

Address (Left Value) Versus Content (Right Value) ............................................176

The Address-of Operator (&) ..........................................................................177

Declaring Pointers................................................................................................179

The Dereference Operator (*) ........................................................................182

Null Pointers ..................................................................................................183

Updating Variables via Pointers ..........................................................................183

Pointing to the Same Memory Location..............................................................184

Summary ..............................................................................................................186

Q&A ....................................................................................................................187

Workshop ............................................................................................................188

Quiz ................................................................................................................188

Exercises ........................................................................................................188

Hour 12 Understanding Arrays

189

What Is an Array? ................................................................................................190

Declaring Arrays ............................................................................................190

Indexing Arrays ..............................................................................................190

Initializing Arrays ..........................................................................................191

The Size of an Array............................................................................................192

Arrays and Pointers..............................................................................................194

Displaying Arrays of Characters ....................................................................196

The Null Character (‘\0’)..............................................................................198

Multidimensional Arrays ....................................................................................199

00 067231861x FM 4.10.2000 10:53 AM Page x

x

Sams Teach Yourself C in 24 Hours

Unsized Arrays ....................................................................................................201

Summary ..............................................................................................................203

Q&A ....................................................................................................................203

Workshop ............................................................................................................204

Quiz ................................................................................................................204

Exercises ........................................................................................................205

Hour 13 Manipulating Strings

207

Declaring Strings ................................................................................................208

What Is a String? ............................................................................................208

Initializing Strings ..........................................................................................208

String Constants versus Character Constants ................................................209

How Long Is a String? ........................................................................................212

The strlen() Function ..................................................................................212

Copying Strings with strcpy()......................................................................213

Reading and Writing Strings ..............................................................................215

The gets() and puts() Functions..................................................................215

Using %s with the printf() Function ............................................................217

The scanf() Function ....................................................................................217

Summary ..............................................................................................................219

Q&A ....................................................................................................................220

Workshop ............................................................................................................221

Quiz ................................................................................................................221

Exercises ........................................................................................................221

Hour 14 Understanding Scope and Storage Classes

223

Hiding Data..........................................................................................................224

Block Scope....................................................................................................224

Nested Block Scope........................................................................................225

Function Scope ..............................................................................................226

Program Scope................................................................................................227

The Storage Class Specifiers ..............................................................................229

The auto Specifier ..........................................................................................229

The static Specifier ......................................................................................230

File Scope and the Hierarchy of Scopes ........................................................232

The register Specifier ..................................................................................233

The extern Specifier ......................................................................................233

The Storage Class Modifiers ..............................................................................234

The const Modifier ........................................................................................234

The volatile Modifier ..................................................................................235

Summary ..............................................................................................................236

Q&A ....................................................................................................................237

00 067231861x FM 4.10.2000 10:53 AM Page xi

Contents

xi

Workshop ............................................................................................................238

Quiz ................................................................................................................238

Exercises ........................................................................................................239

Part IV Functions and Dynamic Memory Allocation

241

Hour 15 Working with Functions

243

Declaring Functions ............................................................................................244

Declaration Versus Definition ........................................................................244

Specifying Return Types ................................................................................244

Using Prototypes ............................................................................................245

Making Function Calls ..................................................................................245

Prototyping Functions..........................................................................................247

Functions with No Arguments........................................................................248

Using time(), localtime(), and asctime()..................................................249

Functions with a Fixed Number of Arguments ..............................................251

Prototyping a Variable Number of Arguments ..............................................251

Processing Variable Arguments ......................................................................252

Learning Structured Programming ......................................................................255

Summary ..............................................................................................................255

Q&A ....................................................................................................................256

Workshop ............................................................................................................257

Quiz ................................................................................................................257

Exercises ........................................................................................................257

Hour 16 Applying Pointers

259

Pointer Arithmetic................................................................................................259

The Scalar Size of Pointers ............................................................................260

Pointer Subtraction ........................................................................................263

Pointers and Arrays..............................................................................................264

Accessing Arrays via Pointers........................................................................264

Pointers and Functions ........................................................................................266

Passing Arrays to Functions ..........................................................................266

Passing Pointers to Functions ........................................................................268

Passing Multidimensional Arrays as Arguments............................................270

Arrays of Pointers ................................................................................................272

Pointing to Functions ..........................................................................................274

Summary ..............................................................................................................276

Q&A ....................................................................................................................276

Workshop ............................................................................................................277

Quiz ................................................................................................................277

Exercises ........................................................................................................278

00 067231861x FM 4.10.2000 10:53 AM Page xii

xii

Sams Teach Yourself C in 24 Hours

Hour 17 Allocating Memory

279

Allocating Memory at Runtime ..........................................................................280

The malloc() Function ........................................................................................280

Releasing Allocated Memory with free() ..........................................................283

The calloc() Function ........................................................................................286

The realloc() Function ......................................................................................288

Summary ..............................................................................................................291

Q&A ....................................................................................................................292

Workshop ............................................................................................................293

Quiz ................................................................................................................293

Exercises ........................................................................................................294

Hour 18 Using Special Data Types and Functions

295

The enum Data Type ............................................................................................296

Declaring the enum Data Type ........................................................................296

Assigning Values to enum Names ..................................................................296

Making typedef Definitions................................................................................300

BOOK: Sams Teach Yourself C in 24 Hours
6.21Mb size Format: txt, pdf, ePub
ads

Other books

The Girl From Over the Sea by Valerie K. Nelson
No Longer Mine by Shiloh Walker
Hearts by Hilma Wolitzer
Amnesia by G. H. Ephron
Space by Emily Sue Harvey
Fighting for the Dead by Nick Oldham
Hyperthought by M M Buckner
Oblivion by Sasha Dawn