DOC PREVIEW
Rose-Hulman CSSE 332 - Lecture Notes

This preview shows page 1-2-3-4 out of 13 pages.

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

Unformatted text preview:

CSSE 332 Functions, Pointers in CFunctions - why and how ?Slide 3Slide 4Pointers made easyDereferencing a pointerPointer operationsSlide 8Pointers and arraysHow arrays and pointers relatePointer arithmeticSummary of Arrays and PointersHomework 4CSSE 332Functions, Pointers in C2Functions - why and how ?If a problem is largeModularization – easier to: •code•debugCode reusePassing arguments to functions–By valueReturning values from functions–By value3Functions – basic exampleExample 9#include <stdio.h>/* 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 */}4Memory layout and addresses 5 10 12.5 9. 8 r sint x = 5, y = 10;float f = 12.5, g = 9.8;char c = ‘r’, d = ‘s’;4300 4304 4308 4312 4316 4317 x y f g c d5Pointers made easy?f4300float f; // variable that stores a float?f_addr4304float *f_addr; // pointer variable that stores the address of a floatf_addr = &f; // & = address operator? 4300ff_addr4300 4304NULLDereferencing a pointer6*f_addr = 3.2; // indirection operator or dereferencing ff_addr4300 43043.2 43003.2g4308float g=*f_addr; // indirection: g is now 3.2f = 1.3;ff_addr4300 43041.3 43007Pointer operationsCreation–int iVal, *ptr, *iPtr; –ptr = &iVal; // pointer assignment/initialization–iPtr = ptr; Pointer indirection or dereferencing–iVal = *ptr; *ptr is the int value pointed to by ptr8#include <stdio.h>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 109Pointers and arraysint 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 valuep, &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)How arrays and pointers relateint 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 instead1011Pointer arithmeticint 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.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 understand12Homework 4 Another exercise on C. Available on class’s Angel page– follow link from the schedule


View Full Document

Rose-Hulman CSSE 332 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?