Pointers in C_ A Hands-On Approach [Toppo & Dewan 2013-12-19].pdf

(3688 KB) Pobierz
1382017711.034.png 1382017711.045.png 1382017711.051.png 1382017711.052.png 1382017711.001.png 1382017711.002.png 1382017711.003.png 1382017711.004.png 1382017711.005.png 1382017711.006.png
 
 
 
 
 
 
 
1382017711.007.png 1382017711.008.png 1382017711.009.png 1382017711.010.png
 
1382017711.011.png
 
1382017711.012.png
 
1382017711.013.png
 
1382017711.014.png 1382017711.015.png 1382017711.016.png 1382017711.017.png 1382017711.018.png 1382017711.019.png 1382017711.020.png 1382017711.021.png 1382017711.022.png 1382017711.023.png
 
 
 
 
 
 
1382017711.024.png 1382017711.025.png 1382017711.026.png 1382017711.027.png 1382017711.028.png 1382017711.029.png 1382017711.030.png
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
1382017711.031.png 1382017711.032.png 1382017711.033.png 1382017711.035.png 1382017711.036.png 1382017711.037.png 1382017711.038.png 1382017711.039.png 1382017711.040.png 1382017711.041.png 1382017711.042.png 1382017711.043.png 1382017711.044.png 1382017711.046.png 1382017711.047.png 1382017711.048.png
Contents at a Glance
About the Authors������������������������������������������������������������������������������������������������������������� xiii
Acknowledgments ������������������������������������������������������������������������������������������������������������� xv
Introduction ��������������������������������������������������������������������������������������������������������������������� xvii
Chapter 1: Memory, Runtime Memory Organization, and Virtual Memory
����������������������� 1
Chapter 2: Pointer Basics
����������������������������������������������������������������������������������������������� 27
Chapter 3: Pointer Arithmetic and Single Dimension Arrays
������������������������������������������ 43
Chapter 4: Pointers and Strings
������������������������������������������������������������������������������������� 57
Chapter 5: Pointers and Multidimensional Arrays
���������������������������������������������������������� 71
Chapter 6: Pointers to Structures
���������������������������������������������������������������������������������� �� 89
Chapter 7: Function Pointers
���������������������������������������������������������������������������������������� 113
Chapter 8: Pointers to File I/O
�������������������������������������������������������������������������������������� 123
Index�������������������������������������������������������������������������������������������������������������������������������� 143
v
1382017711.049.png
Introduction
Ever since the introduction of the C programming language in 1978,it has been regarded as a powerful language
and has gained popularity among programmers worldwide. Despite starting as a language for the UNIX operating
system, it has been used extensively in implementing wonderful and very complex software on multiple platforms.
C has always been the default choice of language for writing any low level layers, device drivers, embedded system
programming, programming mobile devices and so on.
One of most important features of C is pointers , which is an interesting topic and many times diicult to grasp.
C being a relatively low level language, requires that programmers are well versed with many fundamental notions of
computers while using it. And also, C is not a strongly-typed language.
he concept of pointer is known for its cryptic nature and that makes the understanding of it in some cases very
diicult. his book is meant to provide an understanding of the concept of pointers for a novice or an intermediate
or an expert programmer. To make the reader understand any concept of pointers we have provided back ground
information which is not related to the language but which is part of the computer science literature. his background
information will help the reader to understand the concepts very easily.
he book is organized as follows.
Chapter 1 is the basis for other chapters. It describes the concept of memory and runtime memory which
provides the reader with an understanding of the basic concept of how memory is accessed and how data/
instructions are stored in memory. This chapter helps in understanding the compilation steps. This includes
explanation of how intermediate results such as preprocessing, assembly and object code are generated. It also
gives detailed background of how memory segments/sections are created by the compiler. Memory segments
are explained in detail with pros and cons which will help readers to understand the usage of various kinds of
variables. This chapter is also augmented with the understanding of the concept of virtual memory.
Chapter 2 introduces the concept of a pointer variable and the most important operations on it (referencing and
dereferencing). his chapter explains the concept of initialization, comparison and memory allocation to pointer
variables. It also explains the notion of a NULL pointer, dangling pointer, VOID pointer and CONST qualiiers. his
chapter also explains the notion of how a pointer variable is used with diferent types of primitive data types such as
integer, char and so on. his chapter also provides an explanation of how multilevel pointers can be used to access
memory addresses and the values stored at those locations.
Chapter 3 contains a detailed explanation of pointer arithmetic and single dimensional arrays. Pointer arithmetic
is explained in detailed. Explanation is given on how pointers can be used to access various contiguous memory
locations using addition and subtraction operations on pointers. A section in this chapter explains the usage of
pointers to access array data types. his chapter gives illustrious insight on how various kinds of expressions can be
used to access a particular index of an array.
Chapter 4 contains an explanation of how pointers can be used to initialize static strings and manipulate
them. Many examples have been included in the form of basic string manipulation functions such as strcpy,
substring and so on. String manipulation is one of the most important requirements while solving and
implementing algorithms.
Chapter 5 describes the usage of pointers to access multidimensional memory access, specifically 2-d and
3-d arrays.
xvii
1382017711.050.png
IntroduCtIon
Chapter 6 is about the detailed description of how structures and its member ields can be accessed with
pointers. Usage of structures and pointers helps in implementing complex and dynamic data structures. Illustrious
examples have been included in the chapter to explain the implementation of data structures such as linked lists
and binary trees with the help of pointers. A section is also dedicated to explain how a function of a program can be
accessed dynamically with the help of function pointers.
Chapter 7 is an explanation of usage of the function pointers concept.
Chapter 8 contains details about ile handling. How ile pointers are used to manipulate iles using write and read
system calls have been explained in depth.
xviii
Zgłoś jeśli naruszono regulamin