Download Pointers - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!
Pointers
Lecture 14
POINTERS
- Pointers are variables that contain memory addresses as their values.
- A variable name directly references a value.
- A pointer indirectly references a value. Referencing a value through a pointer is called indirection.
- A pointer variable must be declared before it can be used.
POINTERS
- Examples of pointer declarations:
FILE *fptr; int *a; float *b; char *c;
- The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.
POINTERS
#include <stdio.h> int main ( ) { FILE *fptr1 , fptr2 ; / Declare two file pointers */ int aptr ; / Declare a pointer to an int */ float bptr ; / Declare a pointer to a float / int a ; / Declare an int variable / float b ; / Declare a float variable */
POINTERS
fprintf ( fptr2, "%d %d\n" , aptr , bptr ) ; fprintf ( fptr2, "%d %f\n" , *aptr , *bptr ) ; fprintf ( fptr2, "%d %f\n" , a , b ) ; fprintf ( fptr2, "%d %d\n" , &a , &b ) ; return 0 ; }
Assuming that the above is part of a program that runs without error and the the input file does open, what would be printed to the file Docsity.com
Use of & and *
- When is & used?
- When is * used?
- & -- "address operator" which gives or produces the memory address of a data variable
- -- "dereferencing operator" which provides the contents in the memory location specified
Pointers and Functions
- Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.
- We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap.
Pointers and Functions
- If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables.
- The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returnedDocsity.com
Arithmetic and Logical Operations
on Pointers
- A pointer may be incremented or decremented
- An integer may be added to or subtracted from a pointer.
- Pointer variables may be subtracted from one another.
Arithmetic Operations on Pointers
- When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to.
- For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: Docsity.com
Problem G
- A bubble sort can be used to sort the wrist pins in order of size from smallest to largest.
- Six volunteers are needed for a demonstration of the bubble sort.
- The students will be sorted on the basis of height.
- Start with the first person and compare heights with the second person.
- If the first is taller, they swap places. Docsity.com
Problem G
- This problem deals with the wrist pin data from G10 so you might want to use that program to start G12.
- You want to sort the data until it goes from the smallest to the largest.
- You start at the first wrist pin and compare it to the next pin. If it is larger, swap the pins.
- Continue to do this until you have made one pass through the data set.
Problem G
- This is a good application for a do - while loop.
- But what is the test?
START Flow Chart Declare Varsfinptr,foutptr wpin[20],tempi,changes,x Open Files fin NULL Print “did notopen”
Read in data
Y N
Actions ofagorithm
Print sorted list
END