DOC PREVIEW
IUPUI CSCI 23000 - Pointers Introduction

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

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

Unformatted text preview:

Slide 1What is PointerSlide 3Slide 4Pointer Variable Declarations and InitializationSlide 6Pointer Variable DeclarationsPointer Variable InitializationPointer OperatorsPointer OperatorsSlide 11Slide 12Dale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230PointersIntroductionIntroductionDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] RobertsWhat is PointerWhat is PointerBring yourmoney toIUPUI SL-280to exchangehostage“FindInstructionUnderThe crabappletree next to oldlaw school ”“Find nextmessageon the top oftraffic lighton MichiganSt. andWest St.Phone rings and says“Deliver ransom toI-65 and West St. intersectionHostageLocation A: Highway IntersectionLocation B: Traffic LightLocation C:CrabappleTreeLocation D:IUPUI SL-280CS Dept.CS A B C DAddress of next locationA pointer is an address.Dale RobertsWhat is PointerWhat is PointerPig Pen points to Lucy; Lucy points to Sally; Sally points to Linus;Linus points to Charlie; Charlie points to Snoopy;Snoopy has WoodstockExample: Find out who is with Woodstock among peanut gang?SnoopyCharlieLinus…Pig Pen“ Lucy knows ”Lucy“Sally knows”Sally“ Linus knows ”Linus“ Charlie knows ”Charlie“ Snoopy knows ”WoodstockWoodstock is with SnoopySnoopyAn instance uses a pointer to link to a next instance making a chain.Dale RobertsPointer declarations:Pointer declarations:datatype snoopy, *charlie, **linus;snoopy = ; /* snoopy’s content is Woodstock */charlie = &snoopy; /* charlie’s content is the info (pointer) to locate snoopy, which is snoopy’s address*/linus = &charlie; /* linus’s content is the info (pointer) to locate charlie, which is charlie’s address*/In general, we can rewriteIn general, we can rewrite charlie charlie using variable ofusing variable of snoopyPtr snoopyPtr (Snoopy’s pointer)(Snoopy’s pointer) and rewriteand rewrite linus linus using variable ofusing variable of snoopyPtrPtr snoopyPtrPtr (pointer to Snoopy’s pointer); (pointer to Snoopy’s pointer); Note that this is only a naming convention. The only requirement is Note that this is only a naming convention. The only requirement is that the variable be a valid identifier: begin with a letter followed by that the variable be a valid identifier: begin with a letter followed by letters, digits or _.letters, digits or _.Dale RobertsPointer Variable Declarations and InitializationPointer Variable Declarations and InitializationPointers Pointers Definition:Definition:A pointer is a variable that contains address of A pointer is a variable that contains address of another variable.another variable.A powerful feature of C, but difficult to masterA powerful feature of C, but difficult to masterPointers enable programs to Pointers enable programs to simulatesimulate call-by- call-by-reference and create/manipulate dynamic data reference and create/manipulate dynamic data structures structures Close relationship with arrays and stringsClose relationship with arrays and stringsDale RobertsPointer variablesPointer variablesContain memory addresses as their valuesContain memory addresses as their valuesNormal variables contain a specific value (direct reference)Normal variables contain a specific value (direct reference)Pointers contain address of a variable that has a specific Pointers contain address of a variable that has a specific value (indirect reference)value (indirect reference)Indirection Indirection –– referencing a pointer value referencing a pointer valuecount7count7countPtrDale RobertsPointer DeclarationsPointer Declarationstype *variable_nametype *variable_name ** used with pointer variables used with pointer variablesExampleExample:: int *myPtr;int *myPtr;Declares a pointer to an Declares a pointer to an intint (pointer of type (pointer of type int *int *))Multiple pointers require using a Multiple pointers require using a ** before each variable before each variable declarationdeclarationCan declare pointers to any data typeCan declare pointers to any data typeExampleExample::int *myPtr1, *myPtr2;int *myPtr1, *myPtr2;float *pq;float *pq;char *pc;char *pc;Pointer Variable DeclarationsPointer Variable DeclarationsDale RobertsPointer Variable InitializationPointer Variable InitializationInitialize pointers to Initialize pointers to 00, , NULLNULL, or an address, or an address00 or or NULLNULL –– points to nothing ( points to nothing (NULLNULL preferred) preferred)ExampleExample::int *ptr, x;int *ptr, x;x=10;x=10;ptr = &x;ptr = &x;5000ptrFFFFxFFFF5000ptrx10FFFF5000ptrFFFFx10Dale RobertsPointer OperatorsPointer Operators&&: Address Operator: Address OperatorReturns address of operandReturns address of operandint y = 5;int y = 5;int *yPtr; int *yPtr; yPtr = &y;yPtr = &y;/* yPtr /* yPtr “points to”“points to” y */ y *//* yPtr /* yPtr gets address ofgets address of y */ y */yptr500000 600000y6000005yPtry5address of y is the value of yPtrDale RobertsPointer OperatorsPointer Operators** : Indirection / De-referencing Operator : Indirection / De-referencing OperatorReturns a synonym/alias of what its operand Returns a synonym/alias of what its operand points topoints to *yptr*yptr returns returns yy (because (because yptryptr points to points to yy)) ** can be used for assignment that returns alias to can be used for assignment that returns alias to an objectan object*yptr = 7; // changes y to 7*yptr = 7; // changes y to 7Dereferenced pointer (operand of Dereferenced pointer (operand of **) must be a ) must be a variable (no constants)variable (no constants)** and and && are inverses are inverses They cancel each other outThey cancel each other outDale RobertsPointer OperatorsPointer OperatorsExample:int i = 5;int i = 5;int *pi;int *pi;pi = &i; /* place the address of i into pi */pi = &i; /* place the address of i into pi */Assume Symbol TableAssume Symbol Tablei.e. &i = 874, i = 5; &pi = 902, pi = 874;i.e. &i = 874, i = 5; &pi = 902, pi = 874;*pi = ?*pi = ?*pi = 5; *pi = 5; // same as i = 5;// same as i = 5;*pi = *pi * 2; *pi = *pi * 2; // same as i = i * 2;// same as i = i * 2;*pi *= 2; *pi *= 2; // same as i *= 2;// same as i *= 2;8749025pi i874variable address value at the addressi87455pi902874874Dale RobertsExample:int i,*pi,**ppi;int i,*pi,**ppi;i = 5;i = 5;pi = &i;pi = &i;ppi = πppi = π1001041085ppi pi i100104Variable Address


View Full Document

IUPUI CSCI 23000 - Pointers Introduction

Download Pointers Introduction
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 Pointers Introduction 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 Pointers Introduction 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?