DOC PREVIEW
UIUC CS 101 - lect1920

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 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 23 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 23 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 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

19&20-2 • Know how to declare pointer variables. • Understand the & (address) and *(indirection) operators. • Dynamic Memory Allocation • Related Chapter: ABC 6.2 – 6.6, 6.8, 6.10, 6.11, 6.1519&20-3 Pointer variables are variables that store memory addresses. Pointers variables are useful in passing storage addresses in function calls (call-by-reference) and for applications involving dynamic data structures (e.g., linked lists) Example: int *intPtr; float *floatPtr; declares intPtr to be a pointer variable to an object of type integer. and floatPtr to be a pointer variable to an object of type float. The data type for intPtr is, int * ,read as “pointer to an int”. The data type for floatPtr is, float * ,read as “pointer to a float”.19&20-4 We can write the pointer variable declaration as int* intPtr; or as int * intPtr; Note that when we declare more than two pointer variables in one line each pointer name requires an asterisk: int *ptr1, *ptr2; otherwise we are just declaring a regular variable , not a pointer variable.19&20-5 There are two C operators that are necessary when using pointer variables. & - the address operator returns the address of a variable x 1000 3 ptrX 1000 ptrX holds the address of x. We say ptrX “points” to x. We will apply the & operator only to variables, e.g. &77 or &(x+1) are not valid. The declaration of ptrX initializes the variable ptrX = &x; Example: int x = 3; int * ptrX = &x; 100419&20-6 Example: int x; int * pt; pt = &x; /* another way to assign an address to a pointer variable */ Suppose the address of the variable x is 9640 Then, the effect of pt = &x; will be: x pt Address 9640 964019&20-7 int x; int * pt; pt = &x; To assign the value “3” to the variable x in C: x = 3; We can use the “ * ” operator to indirectly reference x. We can assign “3” to x by: *pt = 3; Here we use the fact that “pt” points to integer values. x pt Address 9640 9640 3 * - means "a pointer to" and is called an indirection operator or dereferencing operator, since a pointer "indirectly" references a value in a storage location. Example:19&20-8 double *ptr; ptr = NULL; Assigning a NULL value (zero) to a pointer A null pointer points to nothing. We often depict it as ptr /*called a null pointer*/19&20-9 1. Problem Definition Write a function “swap” that has two input integer arguments. This function should swap the values of the variables. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = two integers Output= no value is returned to the calling function, but the values of the called variables should be swapped. 3. Develop Algorithm Pass the addresses of the variables and not their values. In the swap function the parameters that hold the addresses are pointers. Use the indirection operator to swap the values.19&20-10 /* C Function to swap values. */ #include <stdio.h> void swap(int * , int *); void main(void) /* function header */ { int x = 1; int y = 2; swap(&x,&y); /* pass the addresses of x and y */ printf("x = %i , y = %i \n",x,y); /* prints x = 2 , y = 1 */ } void swap(int *ptrX , int *ptrY) /* pointer variables */ { int temp = *ptrX; *ptrX = *ptrY ; *ptrY = temp; }19&20-11 x 1000 Main Memory before call to swap 1 y 2 Main Memory while executing swap, after temp = *ptrX; but before *ptrX = *ptrY ; 1000 1 ptrX 1000 3000 2 x y 1004 1004 3008 3004 1004 ptrY temp 119&20-12 x y Main Memory while executing swap, after *ptrX = *ptrY ; but before *ptrY = temp; 1000 2 ptrX 1000 3000 2 1004 3008 3004 1004 ptrY temp 119&20-13 x y Main Memory while executing swap, after *ptrY = temp; but before return; 1000 2 ptrX 1000 3000 1 1004 3008 3004 1004 ptrY temp 119&20-14 1. Problem Definition Write a program that reads a list of real numbers into an array from the keyboard (or a file using Unix redirection). The array can be of arbitrary length but the user must first specify the length of the array. The program then calculates the average value, and then prints a list of differences. The differences are computed by taking the original values in the list minus the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = Since the length of the array is specified at run-time we must use Dynamic Memory Allocation. This is accomplished in C by the use of the built-in function calloc (or malloc). Use data-type double to hold the values. Output= The average and the list of differences.#include <stdio.h> #include <stdlib.h> void main(void) { int k,num; double * datValptr; /* pointer used in DMA */ double datAve; double sum = 0.0; /* prompt the user for the number of array elements */ printf("Please enter the number of array elements:"); scanf("%i",&num); /* use calloc to allocate a block of memory */ datValptr = calloc(num,sizeof(double)); /* verify that calloc was successful */ if (datValptr = = NULL) { printf("Memory not allocated!\n"); return; } (continued on next slide)/* read the values and compute the average */ k = 0; for(k=0;k<num;++k) { scanf("%lf", &datValptr[k]);/* use pointer as array name */ sum += datValptr[k]; } /* compute the average */ datAve = sum /num; printf("The average is:%f \n", datAve); /* compute and print the diff list */ for(k=0;k<num;++k) { printf("%f\n", datValptr[k]-datAve); } /* end of for*/ /* free up any memory that was dynamically allocated */ free(datValptr); } /* end of main */19&20-17 unixprompt> ./a.out Please enter the number of array elements: 5 1 2 3 4 5 The average is:3.000000 -2.000000 -1.000000 0.000000 1.000000 2.000000 unixprompt>19&20-18 The array name in C is assigned the address of the first element of the array. The following code will assign the address of x[0] to xPtr : float x[50]; float * xPtr; xPtr = x; The assignment statement above is equivalent to: xPtr = &x[0];19&20-19 In C, we can dynamically allocate storage with either of the following two functions (both are in <stdlib.h>). • malloc(numberOfBytes) • calloc(numberOfItems, itemSize) Both functions return a pointer to the address of the block of storage that has been allocated. If no memory is available, a NULL value will be returned. Function calloc returns a contiguous block of locations that are initialized to 0 and that can be referenced as array


View Full Document

UIUC CS 101 - lect1920

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect1920
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 lect1920 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 lect1920 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?