lect8_1

lect8_1 - Pointer Types - Basics 1 Pointer Types - Basics...

Info iconThis preview shows pages 1–4. 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

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: Pointer Types - Basics 1 Pointer Types - Basics Pointer type is one of the most powerful features of the C++ programming language. We begin with the basic pointer concepts. Pointer declaration Pointers are variables that contain memory addresses as their values. We know that a variable, for example a variable of type int , stores a specific integer value. A pointer, on the other hand, stores the address of a variable. Pointers, like any other variables, must be declared before they can be used. The declaration int *intPtr; creates the pointer intPtr to int . Suppose that the contents of pointer intPtr is the address of an int variable, as shown below. In the above figure, we have dipicted an int variable whose value is 35 and it is located in the memory (the address in memory) at byte number 1000 . The value of the pointer variable intPtr is the address of the int variable. Most of the times, we don’t require the specific memory address, such as 1000 . What is important is that the pointer is holding the address of an int variable located at some address x . It is more convenient and expressive to use an arrow convention, as follows, to illus- trate pointers. We diagram a pointer as an arrow extended from inside the pointer variable intPtr to the target variable. By this we mean that the pointer value is the memory address of the target variable. See figure below. Often, we say that "the pointer is pointing to a certain variable" ; by this we mean that the value of the pointer is the address of the target variable. Pointer Types - Basics 2 We know that, using the variable name, we can directly access the value stored in that variable. If a pointer is pointing to a variable, then we can use the pointer to access (indirectly) the value stored in the variable. In C++, the indirection operator * , enables us to do this. If p is a pointer pointing to a target variable, say a , then *p is the target variable a . We can use *p the same way we use the name a of the target variable. For example, if intPtr is pointing to an int variable as shown below then the statement *intPtr = 45; modifies the contents of the target variable as shown below. When we declare a pointer, we need to specify the data type as follows. int *intPtr; The data type of a pointer in the declaration is essential because an address of a target variable, by itself, just locates the the first byte of the memory location of the variable. When a pointer holds the address of a target variable, the type information is nec- essary to determine the size (number of bytes) occupied by the variable in memory, and the meaning of the bits stored in it. Therefore, the type of a pointer specifies the type of the target variable. Pointer Types - Basics 3 Consider the following declaration....
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 11

lect8_1 - Pointer Types - Basics 1 Pointer Types - Basics...

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

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