Computer Science 210 Computer Organization Pointers.

14
Computer Science 210 Computer Organization Pointers

Transcript of Computer Science 210 Computer Organization Pointers.

Page 1: Computer Science 210 Computer Organization Pointers.

Computer Science 210Computer Organization

Pointers

Page 2: Computer Science 210 Computer Organization Pointers.

The Pointer

• C allows the programmer to access the address of a memory location and use it as a data value

• Pointers are also used to refer to dynamically allocated storage (from the system heap)

Page 3: Computer Science 210 Computer Organization Pointers.

int number = 10;

printf("The value is %d\n", number); // The contents of numberprintf("The address is %p\n", &number); // The address of number

The Address of Operator &

The & operator returns the actual memory address (a large integer, probably) of a variable

The printf format flag p is used to print an address

Page 4: Computer Science 210 Computer Organization Pointers.

int number = 10;int *alias;

Declaring a Pointer Variable

The * operator says that the variable alias can contain a pointer to a memory location that can hold an int

10

number aliasThe memory for alias contains garbage until it is set to a value

Page 5: Computer Science 210 Computer Organization Pointers.

int number = 10;int *alias;

int aCopy = number; // Copies number’s value to aCopy

alias = &number; // Copies number’s address to alias

Assigning an Address

The variables number and aCopy name separate storage locations

The variables number and alias can access the same storage location

10

number aliasThe memory for alias now contains the address of number

10

aCopy

Page 6: Computer Science 210 Computer Organization Pointers.

int number = 10;int *alias = NULL; // Set the pointer variable to empty

int aCopy = number; // Copies number’s value to aCopy

alias = &number; // Copies number’s address to alias

printf("The value is %d\n", number); printf("The value is %d\n", *alias); // Dereference to get value

Access by Indirection (Dereferencing)

The * operator is also the dereference operator

Its operand must be a pointer variable, and it must contain the address of another cell in memory

The value NULL from stdio is used to indicate the empty pointer

Page 7: Computer Science 210 Computer Organization Pointers.

int number = 10;int *alias = NULL; // Set the pointer variable to empty

int aCopy = number; // Copies number’s value to aCopy

alias = &number; // Copies number’s address to alias

(*alias) = 69; // Reset the storage by indirectionprintf("The value is %d\n", number); // Prints 69

Aliasing and Side Effects

* Has a lower precedence than =

*alias and number access the same storage

69

number aliasOuch!!!

10

aCopy

Page 8: Computer Science 210 Computer Organization Pointers.

Input and Output Parameters

• Parameters are passed by value, so they’re good as input-only parameters

• Pointers can be used to implement pass by reference, so they’re good for output parameters (in functions that return multiple values)

• Example: define a function that returns two quadratic roots, given the inputs a, b, and c

Page 9: Computer Science 210 Computer Organization Pointers.

#include <stdio.h>#include <math.h>

int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2);}

Use & with the Actual Parameter

The function quadraticRoots has three input parameters and two output parameters

The address of a variable is passed for each output parameter

The function scanf also uses this mechanism

Page 10: Computer Science 210 Computer Organization Pointers.

#include <stdio.h>#include <math.h>

void quadraticRoots(float a, float b, float c, float *root1, float *root2);

int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2);}

Use * with the Formal Parameter

Input parameters

Output parameters

In the function header, the root1 and root2 parameters are of type pointer (*) to float

This allows them to receive the addresses of float variables when the function is called

Page 11: Computer Science 210 Computer Organization Pointers.

#include <stdio.h>#include <math.h>

void quadraticRoots(float a, float b, float c, float *root1, float *root2);

int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2);}

void quadraticRoots(float a, float b, float c, float *root1, float *root2){ (*root1) = - b + sqrt(b * b - 4 * a * c) / (2 * a); (*root2) = - b - sqrt(b * b - 4 * a * c) / (2 * a);}

Use * to Access or Modify Data

Input parameters

Output parameters

Use dereference (*) to store an integer in the implementation

Page 12: Computer Science 210 Computer Organization Pointers.

Pointers and Arrays

• An array variable is just a name for the base address of the array storage in memory

• Normally, an array variable is not an l-value, meaning that it cannot be the target of an assignment statement

• But, this address can be copied to another variable, which can then be modified

Page 13: Computer Science 210 Computer Organization Pointers.

#include <stdio.h>

int main(){ int i, max = 5, array[max]; for (i = 0; i < max; i++) array[i] = i + 1;

// Print ‘em using the array and the subscript operator for (i = 0; i < max; i++) printf("%d\n", array[i]);}

Process an Array Using []

Page 14: Computer Science 210 Computer Organization Pointers.

#include <stdio.h>

int main(){ int i, max = 5, array[max]; for (i = 0; i < max; i++) array[i] = i + 1;

// Print ‘em using a pointer and dereference int *arrayPtr, *lastAddress = array + max - 1; for (arrayPtr = array; arrayPtr <= lastAddress; arrayPtr++) printf("%d\n", *arrayPtr);

Process the Array Using *

arrayPtr is an alias for array (the address of the first cell)

lastAddress is the address of the last cell in the array structure

arrayPtr is incremented to move through the array structure, and * is used to access each cell