22-11-2012, 05:13 PM
A TUTORIAL ON POINTERS AND ARRAYS IN C
pointers c imp.pdf (Size: 208.33 KB / Downloads: 17)
INTRODUCTION
If you want to be proficient in the writing of code in the C programming language, you
must have a thorough working knowledge of how to use pointers. Unfortunately, C
pointers appear to represent a stumbling block to newcomers, particularly those coming
from other computer languages such as Fortran, Pascal or Basic.
To aid those newcomers in the understanding of pointers I have written the following
material. To get the maximum benefit from this material, I feel it is important that the
user be able to run the code in the various listings contained in the article. I have
attempted, therefore, to keep all code ANSI compliant so that it will work with any ANSI
compliant compiler. I have also tried to carefully block the code within the text. That
way, with the help of an ASCII text editor, you can copy a given block of code to a new
file and compile it on your system. I recommend that readers do this as it will help in
understanding the material.
What is a pointer?
One of those things beginners in C find difficult is the concept of pointers. The purpose
of this tutorial is to provide an introduction to pointers and their use to these beginners.
I have found that often the main reason beginners have a problem with pointers is that
they have a weak or minimal feeling for variables, (as they are used in C). Thus we start
with a discussion of C variables in general.
A variable in a program is something with a name, the value of which can vary. The way
the compiler and linker handles this is that it assigns a specific block of memory within
the computer to hold the value of that variable. The size of that block depends on the
range over which the variable is allowed to vary. For example, on PC's the size of an
integer variable is 2 bytes, and that of a long integer is 4 bytes. In C the size of a variable
type such as an integer need not be the same on all types of machines.
When we declare a variable we inform the compiler of two things, the name of the
variable and the type of the variable. For example, we declare a variable of type integer
with the name k by writing:
Pointer types and Arrays
the compiler will know how many bytes to copy into that memory location pointed to by
ptr. If ptr was declared as pointing to an integer, 2 bytes would be copied, if a long, 4
bytes would be copied. Similarly for floats and doubles the appropriate number will be
copied. But, defining the type that the pointer points to permits a number of other
interesting ways a compiler can interpret code. For example, consider a block in memory
consisting if ten integers in a row. That is, 20 bytes of memory are set aside to hold 10
integers.
Pointers and Strings
The study of strings is useful to further tie in the relationship between pointers and arrays.
It also makes it easy to illustrate how some of the standard C string functions can be
implemented. Finally it illustrates how and when pointers can and should be passed to
functions.
In C, strings are arrays of characters. This is not necessarily true in other languages. In
BASIC, Pascal, Fortran and various other languages, a string has its own data type. But in
C it does not. In C a string is an array of characters terminated with a binary zero
character (written as '\0'). To start off our discussion we will write some code which,
while preferred for illustrative purposes, you would probably never write in an actual
program. Consider, for example: