APS105 Pointers Textbook Chapter5 1 A Broken Swap Example: a function to swap two values void swap(int x, int y) int temp = x; x = y; y = temp; int main() int a = 1, b = 2; swap(a,b); printf( %d %d\n,a,b); // prints: 1 2 "call by value": only copies values, originals are unchanged Also, x and y are in scope of swap, and don't exist outside of swap How can we modify a,b inside swap? 2 Pointers Need a way to refer to locations of things not just a copy of a variable s value a pointer: points to a variable eg., like a house address points to a house Example: Q: I want to deposit $100 into your bank account A: Great! My balance is $25. Q: uuuuuh, I need to know your account number A bank account number is a pointer to an account need to know the account number to update balance 3 RAM random access memory composed of memory chips organized like a giant table of locations Addresses every location in table has an address just like every house on a street use the address to read/write a location In C addresses are stored in pointer variables a pointer is a memory address Address Value 0 0 1 23 2 0 3 0 2 32-2 102 2 32-1 7 4 1
Declaring a Pointer Declaring a pointer variable int *p; // p is of type integer pointer // p is of type pointer to integer // p is of type (int *) Taking the address of a variable int *p int x; // assign to p the address-of x // p points-to x Using Pointers Declaring and assigning int *p; // p is loc w addr 2 int x; // x is loc w addr 3 x = 8; // x = 8 // p = addr-of x Addr Value 0 0 1 23 2 0 3 0 2 32-2 102 2 32-1 7 3 p 8 x NOTE: we usually don't care about address values 5 6 int *p; int x = 8; Dereferencing a Pointer // p points to x int y = *p; // assign to y the value // pointed to by p // * is called a dereference operator // *p means "contents of the location pointed to by p // *p means "what's at p" Example Using Pointers Declaring, assigning, and using a pointer int *p; int x = 8; p x 8 printf("%d\n",*p); // print what's at p // prints 8 7 8 2
double x, y; double *p, *q; x = 3.6; y = 6.7; q = &y; *p = 1.0; *q = *p + 1.0; Pointers Example Addr 0 0 1 0 2 0 3 0 4 0 5 0 2 32-2 0 2 32-1 0 3.6 6.7 2 3 Value 1.0 2.0 x y p q Pointers Example Again double x, y; double *p, *q; x = 3.6; y = 6.7; q = &y; *p = 1.0; *q = *p + 1.0; p q x 3.6 1.0 y 6.7 2.0 9 10 Fixed Swap Example: a function to swap two values void swap(int *p, int *q) int temp = *p; *p = *q; *q = temp; int main() int a = 1, b = 2; swap(&a,&b); printf( %d %d\n,a,b); // prints: 2 1 still "call by value": only copies values, but we're copying address values and the addresses point to the real locations of a and b Functions that Return Pointers Create a function called largeloc Takes addresses of two doubles Returns the address of the larger value Example usage: double x = 2.6; double y = 3.4; double *p; p = largeloc(&x,&y); // p gets set to address of y 11 12 3
largeloc double *largeloc(double *a,double *b) if (*a >= *b) return a; return b; Scope 13 14 Scope of Internal Identifiers Scope of an identifier: the range within which it is recognized an identifier is not recognized outside its scope i.e., it cannot be used, or compiler will complain Ex: for (int i = 1;...;...) scope of i is within the for loop only Scope of a function parameter: only within the body of the function Scope of a variable declared in a function starts at the point of declaration ends at the end of the block it was declared in called an internal identifier 15 Scope Example int foo(int x) int y=5; for (int i=0;i<10;i++) int z = 3; y *= z + x; return y; 16 4
Scope Example Overlapping Scope int foo(int x) int y=5; for (int i=0;i<10;i++) int z = 3; y *= z + x; scope of z scope of i scope of y scope of x int i = 1; printf( i = %d\n,i); int i = 2; printf( i = %d\n,i); printf( i = %d\n,i); return y; What is the output? i = 1 i = 2 i = 1 not recommended!! easy to mess up 17 18 Scope of External Identifiers External identifier one that is declared outside of any function ex: a function identifier/name is external Example: int x; // x is an external identifier void main()... Modular Program Design If external ident. declared before all func.s: then it is called global 19 20 5
Modular Program Design Functions and scope provide modularity can build them independently and combine Modularity eases large programs break the problem into smaller & smaller parts until the parts are small and manageable make these into functions combine them into the whole 21 Example: Mersenne Numbers Prime Number: A num divisble only by 1 and itself is prime 1is not prime Mersenne Number: A prime number M such that M = 2n 1 Where n is also prime Note: There are 48 known Latest discovered 2013 January 25 The largest known prime is Mersenne 2 57,885,161 1 22 Print the first N mersenne primes bool isprime(int x) if (x <= 1) return false; for (int i=2;i<=x/2;i++) if (x % i == 0) return false; return true; bool ismersenne(int x) if (x <= 3) return false; return isprime(x) && isprime((x+1)/2); void printmersenne(int N) for (int i=1;i<n;i++) if (ismersenne(i)) printf("mersenne: %d = %d * 2-1\n",i,(i+1)/2); 23 6