Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Pointers point to memory locations

Similar presentations


Presentation on theme: "Pointers Pointers point to memory locations"— Presentation transcript:

1 Pointers Pointers point to memory locations
Pointers have type that restrict what they point to Pointers are declared in the form type* identifier; Example of pointer declaration: int* p; Note that the type of p is int* or pointer to int p can only point to memory locations (lvalues) of type int Pointers occupy memory and are therefore lvalues Pointers store the address (in memory) of what they point to

2 Pointer Operations Pointers can be assigned
e.g. int* p; int* q; p = q; Pointers can take the address of a variable e.g. int x; int* p; p = &x; & is the address operator which returns the address of its operand Pointers can be dereferenced e.g. int x; int* p; p = &x; *p = 4; * is the indirection operator which returns an lvalue In the example, *p and x are equivalent Note that dereferencing takes time

3 Example int x, y; int* p; p = &x; *p = 4; p = &y; *p = 67.0; 4 67 x y
Note that coercion still occurs because p points to an object of type int

4 Pointer Types Pointer types are fully valid types
Pointer types are ‘nestable’ Valid: int* p; int** q; q = &p; Valid: int x; int* p; int** q; q = &p; *q = &x; Invalid: int x; int* p; int** q; p = &x; q = p; Invalid: int x; int** q; q = &x; Valid: int x; int* p; int*& q = p; q = &x; *q = 4;

5 Pointers and const Pointers can be declared const with two meanings
Pointer pointing to a constant value: const type* p; Pointer pointing to a constant value: type const* p; Constant pointer to a value: type* const p; Example: int x, y; const int z = 4; const int* p; int* const q = &x; p = &y; *p = 4; invalid q = &y; invalid

6 Pointers and Arrays The name of an array can be used as a pointer to its first element e.g. int v[] = {1,2}; int* p = v; *p = 4; A pointer to the first element can also be taken explicitly e.g. int v[] = {1,2}; int* p = &v[0]; *p = 4; Pointers can be incremented to step through an array Example: int v[] = {1,2,3,4}; int* p = v; p points to v[0] p++; p points to v[1] *p = 5; p--; p points to v[0] *p = 6;

7 Sizeof sizeof(type) returns the size of the data type in bytes
The return type of sizeof is size_t, usually an unsigned int sizeof(array identifier) returns the size of the array in bytes On the hp’s the following is true: sizeof(char) == 1 sizeof(short int) == 2 sizeof(int) == 4 sizeof(long int) == 4 sizeof(float) == 4 sizeof(double) == 8 sizeof(int*) == 4

8 Void Pointers Consider the code: int* a; char* b; int* c;
b = (char*)a; c = (int*)b; Before standard C, a == c would usually return true After standard C, a == c did not have to return true (it could) Programmers needed a way to pass pointer types through a generic type Pointer to void (void*) was decided as the generic type Consider the additional code: void* v; v = a; c = (int*)v; After standard C, a == c has to return true

9 Function Pointers Function names are really pointers to the code that implement them Therefore pointers can be declared that store addresses to functions Declaring a function pointer is similar to declaring a function Example: void (*print) (double, double); print is a pointer to a void function with two double parameters Example: void (*print) (double, double); void printMax (double, double); print = printMax; (*print)(4.0,6.0); This would result in an output of 6.0

10 Function Pointers (continued)
The compiler know that you are calling a function in that syntax Therefore, the indirection operator can be omitted print(4.0,6.0); works equally well in the previous example Arrays of function pointers can be declared with the syntax void (*print[3])(double, double) Thus we can now do print[1] = printMax; (*print[1])(6.0,7.0); print[1](6.0,7.0); Note that calling an function pointer that doesn’t point to a valid function will be a runtime error

11 References References can be thought of as constant pointers with two exceptions: References may not actually exist at runtime References are not dereferenced (can be viewed as automatic) Consider the code: void square (void) { int a, c; int& b = a; c = b * a; } The reference to b does not actually exist at run time Why? Because the semantics (meaning) of the function is the same without it

12 References (continued)
Functions can return references Consider our max function: double max (double& a, double& b) { if (a < b) return b; else return a; } Returning double can be expensive, a larger object even more so We can define max to return a reference to the parameter: double& max (double& a, double& b)

13 References (continued)
Notice that this function produces an lvalue Therefore it must also take lvalue’s as parameters The references can all be declared const to avoid this restriction The function can only return references to variables outside its scope Otherwise the reference returned would refer to a non-existant object

14 Character Arrays Two forms of declaring a character array
char colorA[] = “blue”; An initialized character array char* colorB = “blue”; An initialized character pointer Note that the array is allocated in modifiable memory Therefore this is valid: colorA[0] = ‘B’ Note that the character pointer points to constant memory Therefore this is invalid: *colorB = ‘B’ Consider the additional statement: char colorC[5]; Then is valid: colorB = colorC; This is invalid: colorA = colorC; colorA is a constant pointer (array reference)


Download ppt "Pointers Pointers point to memory locations"

Similar presentations


Ads by Google