A Tutorial on Pointers and Arrays in C A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1. 1 (HTML version) July 1998 This material is hereby placed in the public domain Available in various formats via http://www. netcom. com/~tjensen/ptr/cpoint. htm TABLE OF CONTENTS Preface Introduction Chapter 1: What is a Pointer? Chapter 2: Pointer Types and Arrays.
Chapter 3: Pointers and Strings Chapter 4: More on Strings Chapter 5: Pointers and Structures Chapter 6: More on Strings and Arrays of Strings Chapter 7: More on Multi-Dimensional Arrays Chapter 8: Pointers to Arrays Chapter 9: Pointers and Dynamic Allocation of Memory Chapter 10: Pointers to Functions file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%… orial%20on%20Pointers%20and%20Arrays%20in%20C. htm (1 of 2)3/18/2007 12:09:49 AM A Tutorial on Pointers and Arrays in C Epilog file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%… orial%20on%20Pointers%20and%20Arrays%20in%20C. htm (2 of 2)3/18/2007 12:09:49 AM
Preface PREFACE This document is intended to introduce pointers to beginning programmers in the C programming language. Over several years of reading and contributing to various conferences on C including those on the FidoNet and UseNet, I have noted a large number of newcomers to C appear to have a difficult time in grasping the fundamentals of pointers. I therefore undertook the task of trying to explain them in plain language with lots of examples. The first version of this document was placed in the public domain, as is this one. It was picked up by Bob Stout who included it as a file called PTR-HELP.
TXT in his widely distributed collection of SNIPPETS. Since that original 1995 release, I have added a significant amount of material and made some minor corrections in the original work. In this HTML version 1. 1 I’ve made a number of minor changes to the wording as a result of comments emailed to me from around the world. Acknowledgements: There are so many people who have unknowingly contributed to this work because of the questions they have posed in the FidoNet C Echo, or the UseNet Newsgroup comp. lang. c, or several other conferences in other networks, that it would be impossible to list them all.
Special thanks go to Bob Stout who was kind enough to include the first version of this material in his SNIPPETS file. About the Author: Ted Jensen is a retired Electronics Engineer who worked as a hardware designer or manager of hardware designers in the field of magnetic recording. Programming has been a hobby of his off and on since 1968 when he learned how to keypunch cards for submission to be run on a mainframe. (The mainframe had 64K of magnetic core memory! ). Use of this Material: Everything contained herein is hereby released to the Public Domain. Any person may copy or distribute this material in any manner they wish.
The only thing I ask is that if this material is used as a teaching aid in a class, I would appreciate it if it were distributed in its entirety, i. e. including all chapters, the preface and the introduction. I would also appreciate it if, under such circumstances, the instructor of such a class would drop me a note at one of the addresses below informing me of this. I have written this with the hope that it will be useful to others and since I’m not asking any financial remuneration, the only way I know that I have at least partially reached that goal is via feedback from those who find this material useful. ile:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%20on%20Pointers%20and%20Arra ys%20in%20C/Preface. htm (1 of 2)3/18/2007 12:09:49 AM Preface By the way, you needn’t be an instructor or teacher to contact me. I would appreciate a note from anyone who finds the material useful, or who has constructive criticism to offer. I’m also willing to answer questions submitted by email at the addresses shown below. Other versions of this document: In addition to this hypertext version of this document, I have made available other versions more suitable for printing or for downloading of the entire document.
If you are interested in keeping up to date on my progress in that area, or want to check for more recent versions of this document, see my Web Site at http://www. netcom. com/~tjensen/ptr/cpoint. htm Ted Jensen Redwood City, California tjensen@ix. netcom. com July 1998 Continue with Pointer Tutorial Back to Table of Contents file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%20on%20Pointers%20and%20Arrays%20in%20C/Preface. htm (2 of 2)3/18/2007 12:09:49 AM “Introduction” 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. Continue with Pointer Tutorial Back to Table of Contents file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%20on%20Pointers%20and%20Arrays%20in%20C/Introduction. htm3/18/2007 12:09:50 AM Chapter 1 CHAPTER 1: 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: int k; On seeing the “int” part of this statement the compiler sets aside 2 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table.
In that table it adds the symbol k and the relative address in memory where those 2 bytes were set aside. Thus, later if we write: k = 2; we expect that, at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. In C we refer to a variable such as the integer k as an “object”. In a sense there are two “values” associated with the object k. One is the value of the integer stored there (2 in the above example) and the other the “value” of the memory location, i. e. , the address of k.
Some texts refer to these two values with the nomenclature rvalue (right value, pronounced “are value”) and lvalue (left value, pronounced “el value”) respectively. In some languages, the lvalue is the value permitted on the left side of the assignment operator ‘=’ (i. e. the address where the result of evaluation of the right side ends up). The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal. Actually, the above definition of “lvalue” is somewhat modified for C.
According to K II (page 197): [1] “An object is a named region of storage; an lvalue is an expression referring to an object. ” file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%… Pointers%20and%20Arrays%20in%20C/Chapter%2001. htm (1 of 4)3/18/2007 12:09:50 AM Chapter 1 However, at this point, the definition originally cited above is sufficient. As we become more familiar with pointers we will go into more detail on this. Okay, now consider: int j, k; k = 2; j = 7; k = j; age); } /* p points to a structure */ ——————– end of program 5. 2 —————Again, this is a lot of information to absorb at one time.
The reader should compile and run the various code snippets and using a debugger monitor things like my_struct and p while single stepping through the main and following the code down into the function to see what is happening. Continue with Pointer Tutorial Back to Table of Contents file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%… Pointers%20and%20Arrays%20in%20C/Chapter%2005. htm (4 of 4)3/18/2007 12:09:51 AM Chapter 6 CHAPTER 6: Some more on Strings, and Arrays of Strings Well, let’s go back to strings for a bit. In the following all assignments are to be understood as being global, i. e. ade outside of any function, including main(). We pointed out in an earlier chapter that we could write: char my_string[40] = “Ted”; which would allocate space for a 40 byte array and put the string in the first 4 bytes (three for the characters in the quotes and a 4th to handle the terminating ‘