Lecture 5: Pointers in C


Pointer Definition & Operators

Pointer Definition

A pointer is an unsigned short or unsigned long whose value is the address of a place in memory of a particular data type. Two pointers may contain the same address yet not be

f the same type because a pointer is more than just the address it contains. That address must be the start of a place in memory of a certain size and that place in memory must have specific properties. That is, this value must be the address of a variable of a particular data type. This data type may be called the base type of the pointer.

It may seem reasonable that there should be a pointer data type since pointers are just an unsigned short or an unsigned long. However there is more to this relationship than just the address the pointer contains. It is also the size of the memory being pointed to and the properties of that memory.

The Address Operator

It is possible to specify the address of any place in memory by using the address operator & on the variable which names that place in memory. For example:

image from book

 stuffType Stuff; cout << "The address of stuff is "      << &Stuff; 

image from book

will display the address of the variable stuff on the screen. See ADDRESES.CPP which shows the address of several variables of different data types using the address operator.

Before a variable may be a pointer, it must be defined. The pointer definition operator * is used to define a pointer. This operator is read: "is a pointer to an instance of type". For example:

image from book

 stuffType *ptrStuff; 

image from book

defines ptrStuff to be a pointer to an instance of type stuffType

To reference an instance of a data type, the pointer must be initialized. This is done in the following manner:

image from book

       ptrStuff = &Stuff; 

image from book

which is read:

image from book

 ptrStuff "is assigned the address of" Stuff 

image from book

or we could have written this as follows:

image from book

 stuffType *ptrStuff = &Stuff; 

image from book

where the pointer may be defined and initialized in the same statement. See PTRINTLIZ.CPP.

The Indirection Operator

It is possible to use a pointer to access the memory to which the pointer addresses by using the indirection operator: * For example:

image from book

 int anInt; int * ptrInt = &anInt; *ptrInt = 5; 

image from book

the last statement assigns an int value (also called a literal) 5 to anInt. See INDIRECT.CPP. Notice that the symbol * is used for several different operations. You must be careful when you code to keep these differences separate.

When more that one pointer of a particular type is to be defined at the same time, one must be careful how they are defined. For example in the following definition:

image from book

 int *ptrint1, ptrint2; 

image from book

ptrint2 is not a pointer but an int variable instead. If two pointers of the same type are to be defined in the same statement, it is necessary to define them as in the following:

image from book

 int *ptrint1, *ptrint2; 

image from book

The location of the pointer definition operator depends on choice not syntax. Any of the following are syntactically correct:

image from book

 char* ptrChr; int * ptrInt; float *ptrFloat; 

image from book

Passing Pointers into and out of Functions

The first of these is the preferred location. This preferred position is especially true when used with functions. (A pointer as an unsigned int or an unsigned long may be used as input or output to a function.) For example:

image from book

 int f(char* ); 

image from book

Notice how the pointer definition operator is used in the above prototype. Notice how the operator is used when the output is a pointer as in the following function prototype:

image from book

 double * g(int); 

image from book

Pointer Type Casting

Suppose ptr is a pointer of base type: type1 and var is a variable of data type: type2. In general the following assignment should cause a compiler error:

image from book

 type1 * ptr; type2 var; ptr = &var; // syntax error will occur because of this statement 

image from book

However, under certain circumstances, it is possible to store the address of a variable of one data type into a pointer of an entirely different data type by using type casting as in the following example: (but be very careful about doing this because this can introduce memory errors):

image from book

 ptr = (type1*) &var; 

image from book

See pointerconversion.cpp.This type of conversion is used when certain functions are used for file I/O.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net