{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture_pointer - PointersinC Programming...

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

View Full Document Right Arrow Icon
2001-2002: Week 12 1 Pointers in C CSC1500 Computer Principles and  Programming Lecture 12 Supplement Prepared by: Ping-Fu Fung
Image of page 1

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

View Full Document Right Arrow Icon
2001-2002: Week 12 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Image of page 2
2001-2002: Week 12 3 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Image of page 3

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

View Full Document Right Arrow Icon
2001-2002: Week 12 4 Computer Memory Revisited Computers store data in memory slots Each slot has an  unique address Variables store their values like this: Addr Content Addr Content Addr Content Addr Content 1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 1010 1011
Image of page 4
2001-2002: Week 12 5 Computer Memory Revisited Altering the value of a variable is indeed  changing the content of the memory e.g.  i = 40; a[2] = ‘z’; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 1010 1011
Image of page 5

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

View Full Document Right Arrow Icon
2001-2002: Week 12 6 Addressing Concept Pointer stores the  address  of another  entity It  refers  to a memory location int i = 5; int * ptr; /* declare a pointer variable */ ptr = & i; /* store address-of i to ptr */ printf(“*ptr = %d\n”, *ptr ); /* refer to referee of ptr */
Image of page 6
2001-2002: Week 12 7 Why do we need Pointer? Simply because it’s there! It is used in some circumstances in C Remember this? scanf(“%d”, & i);
Image of page 7

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

View Full Document Right Arrow Icon
2001-2002: Week 12 8 What actually  ptr  is? ptr  is a variable storing  an address ptr is  NOT  storing the actual value of i int i = 5; int * ptr; ptr = & i; printf(“i = %d\n”, i); printf(“*ptr = %d\n”, *ptr ); printf(“ptr = %p\n”, ptr ); 5 i address of i ptr Output: i = 5 *ptr = 5 ptr = effff5e0 value of ptr = address of i in memory
Image of page 8
2001-2002: Week 12 9 Twin Operators &: Address-of operator Get the  address  of an entity e.g. ptr = &j; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
Image of page 9

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

View Full Document Right Arrow Icon
2001-2002: Week 12 10 Twin Operators *: De-reference operator Refer to the  content  of the referee e.g. *ptr = 99; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
Image of page 10
2001-2002: Week 12 11 Example: Pass by Reference Modify behaviour in argument passing void 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 = 3 i = 5
Image of page 11

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

View Full Document Right Arrow Icon
2001-2002: Week 12 12 An Illustration int 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 Table Name Type Description Value i int integer variable 5 j int integer variable 10
Image of page 12
2001-2002: Week 12 13 An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern