Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.

Similar presentations


Presentation on theme: "CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization."— Presentation transcript:

1 CSSE 332 Functions, Pointers in C

2 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization – easier to: codecode debugdebug Code reuse Code reuse Passing arguments to functions Passing arguments to functions –By value Returning values from functions Returning values from functions –By value

3 3 Functions – basic example Example 9 #include /* function prototype at start of file */ int sum(int a, int b); int main(int argc, char *argv[]){ int total = sum(4,5); /* call to the function */ printf(“The sum of 4 and 5 is %d\n”, total); } /* the function itself arguments passed by value*/ int sum(int a, int b){ return (a+b); /* return by value */ }

4 4 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317 x y f g c d

5 5 Pointers made easy ? f 4300 float f; // variable that stores a float ? f_addr 4304 float *f_addr; // pointer variable that stores the address of a float f_addr = &f; // & = address operator ?4300 f f_addr 43004304 NULL

6 Dereferencing a pointer 6 *f_addr = 3.2; // indirection operator or dereferencing f f_addr 43004304 3.24300 3.2 g 4308 float g=*f_addr; // indirection: g is now 3.2 f = 1.3; f f_addr 43004304 1.34300

7 7 Pointer operations Creation Creation –int iVal, *ptr, *iPtr; –ptr = &iVal; // pointer assignment/initialization –iPtr = ptr; Pointer indirection or dereferencing Pointer indirection or dereferencing –iVal = *ptr;  *ptr is the int value pointed to by ptr

8 8 #include int main(int argc, char *argv[]) { int j; int *ptr; /* initialize ptr before using it */ ptr=&j; /* *ptr=4 does NOT initialize ptr */ *ptr=4; /* j = 4 */ j=*ptr+1; /* j = ? */ return 0; } Pointer Example 10

9 9 Pointers and arrays int p[10], *ptr; // Although p represents an array, // both p and ptr are pointers, // both p and ptr are pointers, // i.e., can hold addresses. // i.e., can hold addresses. // p is already pointing to a fixed location and // p is already pointing to a fixed location and // cannot be changed. // cannot be changed. // ptr is still to be initialized. // 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)

10 How arrays and pointers relate int a[10]; a[0]a[1]a[9] a: int *pa; pa = &a[0]; // same as pa = a; a[0]a[1]a[9] a: pa: pa + 1: pa + 5: See below instead 10

11 11 Pointer arithmetic int p[10]; ptr = p; // or ptr = &p[0] ptr +=2; // ptr = ptr + 2 * sizeof(int) = ptr+8 bytes => ptr = &(p[2]); p = ptr; Gives ERROR because “p” is a constant address, points to the beginning of an array and cannot be changed.

12 Summary of Arrays and Pointers In C there is a strong relationship between 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 Any operation that can be achieved by array subscripting can be done with pointers The pointer version will be faster, in general The pointer version will be faster, in general –A bit harder to understand 12

13 Homework 4 Another exercise on C. Another exercise on C. Available on class’s Angel page Available on class’s Angel page – follow link from the schedule page 13


Download ppt "CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization."

Similar presentations


Ads by Google