Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and References

Similar presentations


Presentation on theme: "Pointers and References"— Presentation transcript:

1 Pointers and References
CSC136

2 Pointers A pointer is a variable whose value is memory address
The “*” operator is used to create a pointer: float *p; int *p; Pointers have a type When using a pointer, it is assumed that the type data stored at the memory location “pointed to” by the pointer variable is of the pointer type For example: a “float” pointer contains the address of a floating point number

3 Dereferencing a pointer
Dereferencing means retrieving the data from the memory address stored in the pointer Two ways to dereference If “ptr” has been declared as a pointer variable *ptr - use the “*” operator ptr[0] - treat the pointer as an array name and access its first element Array names are actually pointers! int intArray[10]; // declare an integer array intArray is a pointer! Note: can also declare a pointer using [] int ip[]; // is also a pointer, since ip is an (empty) array!

4 Memory allocation: “new” and “delete”
“new” operator allocates a user-specified amount of storage It returns a pointer to the beginning address of the allocated memory Examples float *fp = new float; // one floating point number int *ip = new int[10]; // an array of 10 integers “delete” operator frees the memory allocated by new delete[] ip; // returns memory allocated for int array above to the “heap” // “heap” is a pool of memory available for use – managed by the OS Note: [] is not required if allocated memory was not for an array (ex: delete ptr;)

5 Pointer Arithmetic Examples
Declare: int iarr[10]; int *ip; ip= iarr; Then: *ip is equivalent to ip[0] and iarr[0] *(ip+1) is equivalent to ip[1] and iarr[1] Also: ip++ is equivalent to iarr[1]

6 Address Operator A pointer can be made to “point to” the address of any variable of like type Use the address operator “&” Example: float x=2, y=3; float *p= &x; // creates pointer p and assigns it the memory address of x cout << *p; // will output “2” p= &y; cout << *p; // will output “3”

7 References A reference variable is an “alias” to another variable.
Note it is an “alias” – not a copy! It can be used interchangeably for the variable to which it refers Changing the value of one will also change the value of the other It is created using the “&” operator It must be assigned upon creation (compiler error, if not) It cannot be changed or reassigned after creation (also a compiler error)

8 References, cont. Example:
int y; int &x= y; (can also say int& x=y;) -> x is now a reference to y Useful for passing objects to/from functions and methods No need to construct a new object, just pass its “alias”

9 Pointers vs. References
Similarities Both “refer” to other variables Use of “&” operator with both Significant differences A reference is fixed Serves as an alias to one variable Value of reference is the actual value of referenced variable A pointer is changeable Its value is a memory address Needs to be dereferenced to retrieve usable data

10 Operators “*” and “&” Behavior of both is context dependent
Creates a new pointer when used in variable creation int *p; // creates a new pointer variable Dereferences a pointer variable when used in an expression x= *p; // assigns to x the value located at the mem address stored in pointer p “&” operator Creates a new reference variable when used in variable creation int &p= x; // creates a new reference variable “aliased” to x Retrieves the memory address of a variable when used in an expression p= &x; // assigns to p the memory address of the variable x

11 Usage in Functions All parameters in C/C++ are passed by value to/from functions A copy of the passed variable is made Note: that includes return values! Passing a pointer or a reference the function has access to the original variable The pointer (copy) can be dereferenced So the original can be changed! The reference created on the way in/out is an “alias” to the original variable Again, the original can be changed!

12 Passing Arrays to Functions
Consider: int A[10], B[10], C[]; // Note: could say *C instead of C[] – it is equivalent! C=addArray(A, B, 10); This passes the pointers to the arrays A and B to the function addArray() The pointers are passed by value (which is the default in C++)!!! The pointers can be dereferenced in the function to access the array values That is why array elements can be modified inside functions and no explicit pass by reference is needed. addArray() returns the pointer to a new int array, C (created in the function)

13 Member Access Operator “->”
Used when a pointer “points to” an object or struct A “short cut” notation: pointer->member If: “p” is a pointer to an object… and mem is a member of that object Then: p->mem is equivalent to (*p).mem Note: (*p) is necessary because “.” has higher precedence than “*”


Download ppt "Pointers and References"

Similar presentations


Ads by Google