Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Pointers in C CSSE 332 Operating Systems

Similar presentations


Presentation on theme: "Intro to Pointers in C CSSE 332 Operating Systems"— Presentation transcript:

1 Intro to Pointers in C CSSE 332 Operating Systems
Rose-Hulman Institute of Technology Pass quiz on Pointers. Quiz due at start of next class. 1

2 Announcements Checkout the examples folder (in your svn repo and on the course website Instructions available at Resources => Using SVN on LINUX 2

3 Memory layout and addresses
int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; x y f g c d r s Use the sizeof operator to find the size of char, int, short, float, and double. The result is an unsigned integral constant (size_t)  “%zu” is the portable format specifier for it. sizeof ( type-name )  sizeof (int) static char *strings[] ={ "this is string one", "this is string two", "this is string three", }; sizeof unary-expression  const int string_no = ( sizeof strings ) / ( sizeof strings[0] ); 4300 4304 4308 4312 4316 4317 3

4 Pointers made easy 4300 4304 4300 4304 f f_addr f f_addr
float f; // variable that stores a float float *f_addr; // pointer variable that stores the address of a float f f_addr ? ? NULL 4300 4304 f_addr = &f; // & = address operator [SLIDE ANIMATED] It turns out that addresses are also variables that occupy storage in memory. We will illustrate this using box and pointer diagrams. Figures above depict a variable [ value ] address Assign to f_addr the address of variable f (4300). A pointer is a regular variable, it just holds the address of a variable. A null pointer is a pointer that is guaranteed not to point to any object. Usage: if a routine is to return a pointer and it cannot return a valid pointer, because of error for example, the routine will return a null pointer. f f_addr ? 4300 4300 4304 4

5 Dereferencing a pointer
*f_addr = 3.2; // indirection or dereferencing operator f g f_addr 3.2 4300 3.2 4300 4304 4308 float g=*f_addr; // indirection: g is now 3.2 [SLIDE ANIMATED] To access the variable whose address f_addr holds, you need to dereference f_addr. f = 1.3; f f_addr 1.3 4300 4300 4304 5

6 Pointer operations Creation Pointer indirection or dereferencing
int iVal, value = 8, *ptr, *iPtr; ptr = &value; // pointer assignment & initialization iPtr = ptr; Pointer indirection or dereferencing iVal = *ptr; *ptr is the int value pointed to by ptr [SLIDE ANIMATED] int *ptr, iVal; Only ptr and iPtr are pointers, ival and value are ints. Give students a minute or two to draw box and pointer diagrams for these examples then do the box and pointer diagram on board with class. 6

7 Pointer example, example 10
#include <stdio.h> int main(int argc, char *argv[]) { int j; int *ptr; /* initialize ptr before using it *ptr=4 does NOT initialize ptr */ ptr=&j; *ptr=4; /* j = 4 */ j=*ptr+1; /* j = ? */ return 0; } * has two meanings: 1. declare a pointer dereferencing operator With class, study print statements in example10.c to better understand the behaviour of pointers. Q1 7

8 Pointers and arrays int p[10], *ptr; // Although p represents an array, // both p and ptr are pointers, // i.e., can hold addresses. // p is already pointing to a fixed // location and cannot be changed. // ptr is still to be initialized. p[i] is an int value p, &p[i] and (p+i) are addresses or pointers *p is the same as p[0] (They are both int values) *(p+i) is the same as p[i] (They are both int values) [SLIDE ANIMATED] We will illustrate this graphically in a little while. Adding an integral value to a pointer adds a number of elements. This is best done with arrays. Otherwise, the results can be undefined. 8

9 How arrays and pointers relate
int p[10]; int *pa; pa = &p[0]; //same as pa = p; pa: pa + 1: pa + 5: [SLIDE ANIMATED] int y = *(pa + 1); will copy the content of p[1] into y Why *(pa + 1) instead of *(pa + 4*1) Answer on next slide p: p[0] p[1] p[9] 9

10 Pointer arithmetic ptr = p; // or ptr = &p[0]
ptr +=2; // advances ptr to point to the second integer // that follows where it currently points. => ptr = &(p[2]); p = ptr; Gives error because “p” is a constant address, points to the beginning of an array and cannot be changed. [SLIDE ANIMATED] You cannot just change the address of an array. Q2, 3 10

11 Summary of arrays and pointers
In C there is a strong relationship between arrays and pointers Any operation that can be achieved by array subscripting can be done with pointers The pointer version will be faster, in general A bit harder to understand 11

12 Next Finish the Quiz Do the programming activity (groups of 2 is fine if you like) 12

13 Enjoy Binkey and have fun with *
13


Download ppt "Intro to Pointers in C CSSE 332 Operating Systems"

Similar presentations


Ads by Google