×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

## C

by: Mrs. Damaris Hyatt

14

0

0

# C CSE 20232

Mrs. Damaris Hyatt
ND
GPA 3.79

Staff

These notes were just uploaded, and will be ready to view shortly.

Either way, we'll remind you when they're ready :)

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

COURSE
PROF.
Staff
TYPE
Class Notes
PAGES
0
WORDS
KARMA
25 ?

## Popular in Computer Science and Engineering

This 0 page Class Notes was uploaded by Mrs. Damaris Hyatt on Sunday November 1, 2015. The Class Notes belongs to CSE 20232 at University of Notre Dame taught by Staff in Fall. Since its upload, it has received 14 views. For similar materials see /class/232751/cse-20232-university-of-notre-dame in Computer Science and Engineering at University of Notre Dame.

×

## Reviews for C

×

×

### What is Karma?

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 11/01/15
CSE 20232 333339 Lecture 26 amp 27 Pointers 339 o What is a pointer c amp and operators 0 Using a pointer to access array elements 0 Using pointers to implement reference parameters in C 0 New and delete 0 Using pointers to create dynamically sized arrays Lvalues and Rvalues 3 0 The Ivalue of an object is its address 0 The rvalue of an object is its data value 0 The compiler determines from the CC statement whether to use an object s Ivalue or rvalue Lvalue vs Rvalue example 5 int x y anytime a value is to be assigned to an object the l value address of that object is needed X below is taken to represent the l value of X x 3 anytime the value of an object is needed the r value of that object is used X below is taken to represent the r value of X yx3 notice objects on left of assignments represent their l values and on the right of assignments represent r values What is a pointer 3 o A pointer is an object that has as its value the address of another object 0 Or put another way a pointer is an object that has as its rvalue the lvalue of some other object c We say the pointer point to the other object o Why bother o Pointers facilitate dynamic memory management 0 Pointers can be used to create linear and nonlinear linked data structures lists trees graphs o Swapping pointer values is often more efficient than moving what they point to in momory Pointer Related Operators 3 o The amp operator is used to explicitly return the lvalue of an object 0 Notice this is slightly different than the use of amp in the declaration of reference parameters 0 The operator is used to dereference a pointer and return the rvalue of the object it points to 0 Format of pointer declaration o ltdatatypegt ltpointernamegt Pointer Example 3 declare integers int a3 b 0 Object R Value L Value a 3 1000 b 1004 C 1008 Pointer Example 339 declare integers int a3 b c declare pointers to integers int p q a 3 Object R Value b Pointer Example 339 declare integers int a3 b c declare pointers to integers int p q set b to equal a b 3 b a Object R Value a 3 Pointer Example 3 declare integers int a3 b g I Object R Value L Value declare pelnters to integers int p q a 3 1000 set b to equal a b 3 1004 b a set p to point to a 1008 pampa 1012 1016 Pointer Example declare integers int a3 b c I I Object R Value L Value declare pelnters to integers int p q a 12 1000 set b to equal a b 3 1004 b a set p to point to a 1008 pampa 1012 set what p points to to l2 p 12 1016 Pointer Example declare integers int a3 b g I Object R Value L Value declare pelnters to integers int p q a 12 1000 set b to equal a b 3 1004 b a set p to point to a 1008 p ampa set what p points to to 12 1012 p 12 1016 set q to point to what p does q p Pointer Example declare integers int a3 b c I I Object R Value L Value declare pelnters to integers int p q a 12 1000 set b to equal a b 3 1004 b a set p to point to a 1008 p ampa set what p points to to 12 1012 p 12 1016 set q to pornt to what p does q p move p to next int in memory p Pointer Example declare integers int a3 b c I I Object R Value L Value declare pelnters to integers int p q a 12 1000 set b to equal a b 3 1004 b a set p to point to a 15 1008 p ampa set what p points to to 12 1012 p 12 1016 set q to pornt to what p does q p move p to next int in memory p sum what p amp qpoint to cpq Using pointers to access and array 9 0 Basic steps a Set pointer p to point to first element 0 Use p to set value of the array element that p points to o Advance p to next element p a Note amount of increment is based on the size of what is pointed to sizeofint etc Using a pointer to access 33 array elements 3 declare array of doubles double A5 and declare a loop index I int i 1000 Object R Value L Value 1008 1016 1024 1032 1040 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 pass 1 Object R Value L Value 100 0 1000 1008 1016 1024 1032 1040 Using a pointer to access 33 array elements 3 declare array of doubles double A5 and declare a loop index I int i 100 1000 use loop index to set array all values to 100 for i0 ilt5 i 1016 Ai 1000 pass 2 Object R Value L Value 100 1008 1024 1032 1 1040 Using a pointer to access 33 array elements 3 declare array of doubles double A5 and declare a loop index I int i 100 1000 use loop index to set array all values to 100 for i0 ilt5 i 100 1016 Ai 1000 pass 3 Object R Value L Value 100 1008 1024 1032 2 1040 Using a pointer to access 33 array elements 3 declare array of doubles double A5 and declare a loop index I int i 100 1000 use loop index to set array all values to 100 for i0 ilt5 i 100 1016 Ai 1000 pass 4 Object R Value L Value 100 1008 100 1024 1032 3 1040 Using a pointer to access 33 array elements 3 declare array of doubles double A5 and declare a loop index I int i 100 1000 use loop index to set array all values to 100 for i0 ilt5 i 100 1016 Ai 1000 pass 5 Object R Value L Value 100 1008 100 1024 100 1032 4 1040 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 Object R Value L Value 100 100 100 100 100 5 1000 1008 1016 1024 1032 1040 1044 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 use pointer to scale all array values for i6 ilt10 i p p i p pass 1 Object R Value L Value 600 100 100 100 100 6 1000 1008 1016 1024 1032 1040 1044 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 use pointer to scale all array values for i6 ilt10 i p p i p pass 2 CHI OIHI O O 00 0 Object R Value L Value AO 600 1000 Al 700 1008 f AZ 100 1016 A3 100 1024 A4 100 1032 i 7 1040 p 1008 1044 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 use pointer to scale all array values for i6 ilt10 i p p i p pass 3 O 0 O O 0 0 Object R Value L Value AO 600 1000 A1 700 1008 A2 800 1016 x A3 100 1024 A4 100 1032 i 8 1040 p 1016 1044 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 use pointer to scale all array values for i6 ilt10 i p p i p pass 4 O 0 O O i 0 Object R Value L Value AO 600 1000 A1 700 1008 A2 800 1016 A3 900 1024 A4 100 1032 i 9 1040 p 1024 1044 Using a pointer to access array elements declare array of doubles double A5 and declare a loop index I int i use loop index to set array all values to 100 for i0 ilt5 i Ai 1000 declare pointer to doubles double p set p to point to AO p A or p ampA0 use pointer to scale all array values for i6 ilt10 i p p i p pass 5 lt O 0 O O i 0 Object R Value L Value AO 600 1000 A1 700 1008 A2 800 1016 A3 900 1024 A4 1000 1032 z i 10 1040 p 1032 1044 Using pointers in C to implement 39 reference parameters 39 c There is no true reference parameter passing mode in C only in C 0 In C all function parameters are value parameters 0 References are passed as pointers and then are dereferenced in the function to change what is pointed to Example of C vs C Reference parameters declaration in C using reference parameters void swap intamp a intamp b int temp a a b b temp int int x5 y12 the call in C l Values swap x y now X main 12 amp y declaration in C using pointers void swap int a int b int temp a a b b temp int main int x5 y12 the call in C pass swapampx ampy now X 12 amp y 5 Swapping or changing values of pointers 3 o In C a swap function that swaps pointer values 0 Uses reference parameters for the two pointers o Assigns one pointer the value of the other e They now point to what the other used to 0 However In C a swap function to swap pointer values 0 Must have parameters that are pointers to pointers o The assignments involve double indirection Example of C vs C Swapping pointer values declaration in C using reference parameters void swap intamp a int temp a a b b temp int main int x5 y12 int pampx qampy the call in C swap p q now p points to y and qpoints to X intamp b O 0 O O 1 O declaration in C using pointers void swap int a int b int temp a a b b int main int x5 y12 int pamp qampy the call in C pass l values swapampp ampq now p points to y and q points to X New 3339 o A pointer can be used to point to a dynamically allocated nameless object while the program is running 0 New allocates a nameless object from the free memory store or runtime heap and returns its address 0 NULL is a special value for pointers indicating that it points to nothing 0 New returns NULL if the allocation fails Delete 3 0 Delete is used to return dynamically allocated objects that are no longer needed back to the free memory store 0 Failure to use delete causes memory leaks 0 NULL is a special value for pointers indicating that it points to nothing New and delete example 3 declare a pointer to an int int p allocate an int for the pointer to point to p new int if NULL p if allocation did not fail use the int that p points to cout ltlt Enter your age cin gtgt p cout ltlt Next year you will be ltlt p1 when done free the dynamically allocated memory delete p Dynamically allocated arrays 0 Maximum array size can be set At comgile time using fixed sizes During run time with flexible sizes Use new to allocate int A new intsize note size And use delete when done delete A note required for array Example 553 Read amp alphabetize words 55quot o Given a file randwordstxt containing 0 Line 1 n the number of numbers following o Nextn lines words a Perform the following steps 0 Open file 0 Read number 0 Allocate array just big enough 0 Read amp insertion sort words o Write words out into file diotwordstxt 0 Delete array Sample word files 3 File randmrds txtquot File dictwords txtquot 5 5 make dictionary these into words make into these dictionary words

×

×

### BOOM! Enjoy Your Free Notes!

×

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

Steve Martinelli UC Los Angeles

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Janice Dongeun University of Washington

#### "I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Steve Martinelli UC Los Angeles

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Parker Thompson 500 Startups

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!
×

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com