L5_pointers_arrays

L5_pointers_arrays - 1 Pointers and Arrays For : COP 3330....

Info iconThis preview shows pages 1–2. 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: 1 Pointers and Arrays For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Introduction To Pointers | A pointer in C++ holds the value of a memory address | A pointer's type is said to be a pointer to whatever type should be in the memory address it is pointing to z Just saying that a variable is a pointer is not enough information! | Generic syntax for declaring a pointer: dataType *pointerVarName; | Specific examples int *iPtr; //Declares a pointer called "iPtr" that will //point to a memory location holding an //integer value float *fPtr; //Declares a pointer called "fPtr" that will //contain an address, which is an address of //a memory location containing a float value The Operator & | There is an operator, & z When this symbol is used as a unary operator on a variable it has a different meaning than as a binary operator or type specifier z It is unrelated to the "logical and", which is && or bitwise and, & z It is unrelated to the use of & in regards to reference parameters | The operator & is usually called the "address of" operator | It returns the memory address that the variable it operates on is stored at in memory | Since the result is an address, it can be assigned to a pointer Using The "Address Of" Operator 1008 1009 1011 1010 1007 1006 1005 1004 1003 1002 1001 1000 1000 i iPtr 6 int i = 6; //Declares an int, stored //in memory somewhere //In this example, it is //stored at address 1000 int *iPtr; //Declares a pointer. The //contents of this variable //will point to an integer //value. The pointer itself //must be stored in memory, and //in this example, is stored at //memory location 1004 iPtr = &i; //Sets the iPtr variable to //contain the address of the //variable i in memory The Operator * | Like the &, the * operator has another meaning as well | The * operator is usually referred to as the "dereference operator" | The * operator operates on a pointer value z The meaning is "Use the value that this pointer points to, rather than the value contained in the pointer itself" | If a pointer is of type "int *" and the dereference operator operated on the pointer, the result is a value of type "int" | Dereferenced pointers can be used as L-values or R-values z When used as an L-value (on the left side of an assignment), the pointer is unaffected, but the memory that it points to is changed | When a pointer that is pointing to memory you are not allowed to access is dereferenced, the result is a program crash via a "segmentation fault" Using The Dereference Operator 1008 1009 1011 1010 1007 1006 1005 1004 1003 1002 1001 1000 1000 i iPtr 6 int i = 6; //Declares integer called i int *iPtr; //Declares a pointer to an int iPtr = &i; //Sets the iPtr variable to //contain the "address of" the //variable i in memory cout << "i: " << i << endl; cout << "i: " << *iPtr << endl; *iPtr = 4; //Changes the memory being //pointed to by iPtr to contain //the value 4 cout << "i: " << i << endl;...
View Full Document

Page1 / 8

L5_pointers_arrays - 1 Pointers and Arrays For : COP 3330....

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

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