lecture_pointer - 2001-2002: Week 121Pointers in CCSC1500...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2001-2002: Week 121Pointers in CCSC1500 Computer Principles and ProgrammingLecture 12 SupplementPrepared by: Ping-Fu Fung2001-2002: Week 122Pre-requisiteBasics of the C programming languageData typeVariableArrayFunction callStandard Input/Outpute.g.printf(), scanf()2001-2002: Week 123OutlineComputer Memory StructureAddressing ConceptIntroduction to PointerPointer ManipulationSummary2001-2002: Week 124Computer Memory RevisitedComputers store data in memory slotsEach slot has an unique addressVariables store their values like this:AddrContentAddrContentAddrContentAddrContent1000i: 371001j: 461002k: 581003m: 741004a[0]: a1005a[1]: b1006a[2]: c1007a[3]: \01008ptr: 10011009101010112001-2002: Week 125Computer Memory RevisitedAltering the value of a variable is indeed changing the content of the memorye.g. i = 40; a[2] = z;AddrContentAddrContentAddrContentAddrContent1000i: 401001j: 461002k: 581003m: 741004a[0]: a1005a[1]: b1006a[2]: z1007a[3]: \01008ptr: 10011009101010112001-2002: Week 126Addressing ConceptPointer stores the address of another entityIt refers to a memory locationint i = 5;int *ptr; /* declarea pointer variable */ptr = &i; /* store address-of ito ptr */printf(*ptr = %d\n, *ptr); /* referto referee of ptr */2001-2002: Week 127Why do we need Pointer?Simply because its there!It is used in some circumstances in CRemember this?scanf(%d, &i);2001-2002: Week 128What actually ptr is?ptr is a variable storing an addressptr is NOT storing the actual value of iint i = 5;int *ptr;ptr = &i;printf(i = %d\n, i);printf(*ptr = %d\n, *ptr);printf(ptr = %p\n, ptr);5iaddress of iptrOutput:i = 5*ptr = 5ptr = effff5e0value of ptr = address of iin memory2001-2002: Week 129Twin Operators&: Address-of operatorGet the address of an entitye.g.ptr = &j;AddrContentAddrContentAddrContentAddrContent1000i: 401001j: 331002k: 581003m: 741004ptr: 10011005100610072001-2002: Week 1210Twin Operators*: De-reference operatorRefer to the content of the refereee.g.*ptr = 99;AddrContentAddrContentAddrContentAddrContent1000i: 401001j: 991002k: 581003m: 741004ptr: 10011005100610072001-2002: Week 1211Example: Pass by ReferenceModify behaviour in argument passingvoid f(int j){j = 5;}void g(){int i = 3;f(i);}void f(int *ptr){*ptr= 5;}void g(){int i = 3;f(&i);}i = ?i = ?i = 3i = 52001-2002: Week 1212An Illustrationint i = 5, j = 10;int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3;**pptr = 7;ptr = &j;**pptr = 9;*pptr = &i;*ptr = -2;Data TableNameTypeDescriptionValueiintinteger variable5jintinteger variable102001-2002: Week 1213An Illustrationint i = 5, j = 10;int *ptr; /* declare a pointer-to-integer variable */int **pptr;...
View Full Document

This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Page1 / 39

lecture_pointer - 2001-2002: Week 121Pointers in CCSC1500...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online