More_Than_You_Ever_Wanted_to_Know_About_Pointers_v3

More_Than_You_Ever_Wanted_to_Know_About_Pointers_v3 - More...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More Than You Ever Wanted to Know About Pointers Kyle Lemons November 1, 2006 Contents 1 Introduction 2 2 The Basics 2 2.1 What Is a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.2 Why Use a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.3 Compiler Speak: Syntax . . . . . . . . . . . . . . . . . . . . . . . 4 3 Using Pointers 6 3.1 Whats Behind Door #3: Dereferencing a Pointer . . . . . . . . . 6 3.2 Where are you? The Address-Of Operator . . . . . . . . . . . . . 7 3.3 I Swear Its Really A Pointer: Arrays . . . . . . . . . . . . . . . . 8 3.4 Fun With Strings (C-style) . . . . . . . . . . . . . . . . . . . . . 9 3.5 A Walk in the Park: Traversing Arrays and Strings . . . . . . . . 10 4 Advanced Usage 12 4.1 Here We Go Again: Arrays of Strings . . . . . . . . . . . . . . . 12 4.2 Kill Me Now: Multidimensional Arrays . . . . . . . . . . . . . . . 12 4.3 Pointers to Nowhere: Handles . . . . . . . . . . . . . . . . . . . . 12 4.4 Back to Kindergarten: Pointer Arithmetic . . . . . . . . . . . . . 12 4.5 Data, Data Everywhere: Pointers to Structs . . . . . . . . . . . . 12 5 Memory Management 12 5.1 The usual suspects: malloc and free . . . . . . . . . . . . . . . 12 5.2 Even More Fun With Strings: String Fun ctions . . . . . . . . . . 12 5.3 Array Management: calloc , realloc , and the mem* Family . . . 12 1 1 Introduction This article is written at the request of the Fall 2006 CS1372 class at the Georgia Institute of Technology. The purpose of this document is to supplement the readings and to assist in the understanding of pointers. The motivation behind it is that pointers are such an integral part of and a large component of the power behind programming in C and C++ that not understanding them would cripple the student in the course and impede their effectiveness as a programmer. This document will cover pointers from the very most basic concepts up to the most advanced usage that the author can come up with, with the hope that the reader will work slowly through it to understand each concept before moving on to more difficult ones. All code examples contained in this document should work in any standards- compliant C or C++ compiler. At the moment, the author does not intend to cover the new and delete C++ operators, so most of the text should apply equally to C and C++. 2 The Basics 2.1 What Is a Pointer? At its most basic, a pointer is just a number. In fact, pointers are really just unsigned int egers. In most operating systems (32-bit) a pointer is 32 bits wide. Not coincidentally, this is the same size as an unsigned int eger. In fact, as you can see in the example below 1 , you can print out a pointer using printf and it can look exactly like an integer. Note, however, that there are also special format specifiers which can print out pointers in a more useful manner ( %p , %x and %X ). Granted, they are very large integers, but they are integers just the same. Understand that this is a contrived example and is not very useful, sosame....
View Full Document

Page1 / 12

More_Than_You_Ever_Wanted_to_Know_About_Pointers_v3 - More...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online