DOC PREVIEW
Purdue CS 15900 - Chapter 9 Pointers

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 9PointersCS 159 - C ProgrammingPointers▪ A variable’s address is the first byte occupied by the variable▪ The address that a variable is assigned is not predictableNameAddressValuez 1407351465x 140735161y 1407352020Pointer – a constant or variable that contains an address that can be used to access data.int x = 1;int y = 20;char z = 'A';Pointers▪ A pointer variable points to another location in memoryNameAddressValuez 1407351465x 140735161y 1407352020p 1407352414073516int x = 1;int y = 20;char z = 'A';int *p = &x;Pointers▪ Any of these pointer declarations are valid:int* x;int *x;int * x;Pointers▪ A pointer (just like any other variable) cannot be used until it is assigned.▪ A pointer can be assigned the initial value of NULLint x; // contains an unknown value int *p; // contains an unknown addressx = x + 1;*p = *p + 1;int *p = NULL;Pointer Operators&*inverseAddressOperatorIndirectionOperatorPointer OperatorsThe address operator & will return the memory address of a variable▪ Previously used with the scanf function which receives an address list▪ Previously used with passing by addressscanf("%d",&num);assignData(&num);int x = 4;printf("The value of x: %d\n", x);printf("The address of x: %d\n", &x);The value of x: 4The address of x: 14073516Pointer OperatorsThe indirection operator * will return the value that is stored at a memory address (dereferencing)▪ Previously used with passing by addressint x = 4;int *p;p = &x;printf("The value of p: %d\n", p);printf("The value at address p: %d\n", *p);The value of p: 14073516The value at address p: 4void assignData(int *num){*num = 25;}PointersA pointer variable can be used to change the contents of a variable#include <stdio.h>int main(void){int x = 4;int *p;p = &x;*p = 5;printf("The value of x: %d\n", x);printf("The value at address p: %d\n", *p);return 0;}The value of x: 5The value at address p: 5PointersMultiple pointer variables can point to the same variableint x = 4;int *p;int *q;p = &x;q = p;*p = 7;*q = 5;printf("The value of x: %d\n", x);printf("The value at address p: %d\n", *p);printf("The value at address q: %d\n", *q);The value of x: 5The value at address p: 5The value at address q: 5xpqPointersLayers of indirection can be created with pointersint x = 5;int *p;int **q;p = &x;q = &p;printf("The value of x: %d\n",x);printf("The value at address p: %d\n",*p);printf("The value at the value at address q: %d\n",**q);The value of x: 5The value at address p: 5The value at the value at address q: 5xpqPointers▪ The size of any pointer represents one memory address, but the size of the variable it references depends on its data type▪ You cannot assign pointers of different types without casting themchar c;char *p;int *q;p = &c;q = (int*)p; // this is a valid statementq = p; // this will result in an error#include <stdio.h>int main(void){int x = 1;int y = 2;int *p;int *q;p = &x;q = &y;*q = y + (*p)++;*&x += *q - y;printf("The value of x: %d\n", x);printf("The value of y: %d\n", y);return 0;}#include <stdio.h>int main(void){int x;int y = 2;int *p;int *q = &y;p = &x;*p = 3;q++;*q = 4;printf("The value of x: %d\n", x);printf("The value of y: %d\n", y);return 0;}Is this okay since x is not initialized?#include <stdio.h>int main(void){long int x = 5;long int *p;p = &x;x = (long int) p;printf("%ld\n",x);printf("%ld\n",*p);printf("%p\n",p);return 0;}Pointers▪ Pointers are generally considered a dangerous programming construct and are often discouraged unless absolutely necessary▪ Use with extreme caution“[Hackers] inevitably demand a powerful workstation with local disk onto which they'll put a couple of hundred megabytes of unstructured, incoherent pointers all of which point to the number 42; any attempt to read or write it usually results in the network being down for a week at least.


View Full Document

Purdue CS 15900 - Chapter 9 Pointers

Download Chapter 9 Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 9 Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 9 Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?