C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Programming and Data Structure
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Pointers Ethan Cerami Fundamentals of Computer New York University.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Computer Science 210 Computer Organization Pointers.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 17: The Last Puzzle Piece with Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers in C.
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Pointers  Week 10.
Systems Programming Concepts
Pointers Call-by-Reference CSCI 230
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
CISC181 Introduction to Computer Science Dr
C Pointers Systems Programming.
Presentation transcript:

C Pointers Systems Programming Concepts

PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer Example  Pointers and Arrays  Operator Precedence Example Systems Programming Pointers 2

Variables   Variable names correspond to memory locations in memory. Every variable has a type, a name and a value. int i; i i = 4; ( the address of i ) &i 4 Systems Programming Pointers 3

Print an Address int main () { int i; int i; i = 4; i = 4; printf(‘i = %d, address of i = %u\n”, i, &i); printf(‘i = %d, address of i = %u\n”, i, &i); return 0; return 0;} $./ptr1 i = 4, address of i = Systems Programming Pointers 4

Pointers  What is a pointer? – –a variable that contains a memory address as its value. – –Pointers contain the address of a variable that has a specific value (an indirect reference).  typed  Pointers in C are typed. – –a pointer to a variable of type int – –a pointer to a variable of type char – –a pointer to a defined type or an object.  2007 Pearson Ed -All rights reserved. Systems Programming Pointers 5

Fig. 7.1 Directly and Indirectly Referencing a Variable  2007 Pearson Ed -All rights reserved. Systems Programming Pointers 6

Pointers /* Welcome to the world of Pointers! Pointers are a powerful tool */ int main () { int i; int i; int *ptr; /* pointer declaration */ int *ptr; /* pointer declaration */ i = 4; i = 4; ptr = &i; ptr = &i; printf(" i = %d\n address of i = %u\n address of pointer = %u\n", printf(" i = %d\n address of i = %u\n address of pointer = %u\n", i, ptr, &ptr); i, ptr, &ptr); return 0; return 0;}./ptr2 i = 4 address of i = address of pointer = Systems Programming Pointers 7

Pointers /* Do you think in Hex ?*/ int main () { int i; int i; int *ptr; int *ptr; i = 4; i = 4; ptr = &i; ptr = &i; printf(" i = %d\n address of i = %p\n address of pointer = %p\n", printf(" i = %d\n address of i = %p\n address of pointer = %p\n", i, ptr, &ptr); i, ptr, &ptr); return 0; return 0;}./ptr3 i = 4 address of i = 0xbfe07244 address of pointer = 0xbfe bfe07244 bfe07240 ptr i Systems Programming Pointers 8

Pointers /* Never trust a Compiler. */ int j, i; /* think globally! */ int *ptr1, *ptr2; void printit () { printf(" i = %2d, ptr1 = %p\n", i, ptr1); printf(" j = %2d, ptr2 = %p\n", j, ptr2); } int main () { i = 4; j = 8; ptr1 = &i; ptr2 = &j; printit (); *ptr2 = *ptr2 + 1; ptr1 = ptr1 - 2; /* You cannot know this */ printit (); i = 6; *ptr1 = *ptr1 + 10; printit (); return 0; }./ptr4 i = 4, ptr1 = 0x j = 8, ptr2 = 0x804964c i = 4, ptr1 = 0x804964c j = 9, ptr2 = 0x804964c i = 6, ptr1 = 0x804964c j = 19, ptr2 = 0x804964c c ptr1 ptr2 j i The unary (or indirection) operator returns the value of the object to which its operand points. Systems Programming Pointers 9

7.4 Passing Arguments to Functions by Reference   All arguments in C are passed by value!!   Call by reference is done with pointer arguments. – –Pass address of argument using & ( address operator). – –Allows you to change the value of the variable in the caller. – –Arrays are not passed with & because the array name is already a pointer.   * indirection operator – –Used as alias/nickname for variable inside of function void double( int *number ) { *number = 2 * ( *number ); } – – *number used as nickname for the variable passed.  2007 Pearson Ed -All rights reserved. Systems Programming Pointers 10

Using Pointers in Call by Reference Function prototype takes a pointer argument Function cubeByReference is passed an address, which can be the value of a pointer variable. In this program, *nPtr is number, so this statement modifies the value of number itself.  2007 Pearson Ed -All rights reserved. Systems Programming Pointers 11

Swap: A Pointer Example /* A simple memory swap using pointers */ void swap (int *i, int *j) { int temp; int temp; temp = *i; temp = *i; *i = *j; *i = *j; *j = temp; *j = temp;} Systems Programming Pointers 12

Swap: A Pointer Example int main ( ) { int i; int i; int mem1; int mem1; int mem2; int mem2; int ray1[4]; int ray1[4]; mem1 = 12; mem1 = 12; mem2 = 81; mem2 = 81; swap (&mem1, &mem2); /* swap two integers */ swap (&mem1, &mem2); /* swap two integers */ printf("mem1:%4d mem2:%4d\n", mem1, mem2); printf("mem1:%4d mem2:%4d\n", mem1, mem2); for (i = 0; i < 4; i++) for (i = 0; i < 4; i++) { ray1[i] = 10*i; ray1[i] = 10*i; printf("ray1[%d] =%4d ", i, ray1[i]); printf("ray1[%d] =%4d ", i, ray1[i]); } printf("\n"); printf("\n");./swap mem1: 81 mem2: 12 ray1[0] = 0 ray1[1] = 10 ray1[2] = 20 ray1[3] = 30 Systems Programming Pointers 13

Swap: A Pointer Example swap (&mem1, &ray1[3]); swap (&mem1, &ray1[3]); swap (&mem2, &ray1[2]); swap (&mem2, &ray1[2]); printf("mem1:%4d mem2:%4d\n", mem1, mem2); printf("mem1:%4d mem2:%4d\n", mem1, mem2); for (i = 0; i < 4; i++) for (i = 0; i < 4; i++) printf("ray1[%d] =%4d ", i, ray1[i]); printf("ray1[%d] =%4d ", i, ray1[i]); printf("\n"); printf("\n"); return 0; return 0;} mem1: 30 mem2: 20 ray1[0] = 0 ray1[1] = 10 ray1[2] = 12 ray1[3] = 81 Systems Programming Pointers 14

Pointers and Arrays r[0] r[1] r[2] r[3] r[4] r[5] ptr r int main () { int i, r[6] ={1,1,1}; int *ptr; ptr = r; *ptr = 83; *(ptr +2) = 33; for (i=0; i < 6; i++) printf (" r[%d] = %d\n", i, r[i]); Systems Programming Pointers 15

Pointers and Arrays ptr r r[4] = *ptr; ptr++; *ptr = 6; *(ptr +2) = 7; for (i=0; i < 6; i++) printf (" r[%d] = %d\n", i, r[i]); return 0; } Systems Programming Pointers 16 r[0] r[1] r[2] r[3] r[4] r[5]

Pointers and Arrays ptr r r[4] = *ptr; ptr++; *ptr = 6; *(ptr +2) = 7; for (i=0; i < 6; i++) printf (" r[%d] = %d\n", i, r[i]); return 0; } Systems Programming Pointers 17 r[0] r[1] r[2] r[3] r[4] r[5]

Pointers and Arrays ptr r r[4] = *ptr; ptr++; *ptr = 6; *(ptr +2) = 7; for (i=0; i < 6; i++) printf (" r[%d] = %d\n", i, r[i]); return 0; } Systems Programming Pointers 18 r[0] r[1] r[2] r[3] r[4] r[5]

Operator Precedence Example /* An example of operator precedence trouble */ int main () { float x,y,z; float x,y,z; float *ptr1, *ptr2, *ptr3; float *ptr1, *ptr2, *ptr3; x =2.0; y = 8.0; z = 4.0; x =2.0; y = 8.0; z = 4.0; ptr1 = &x; ptr1 = &x; ptr2 = &y; ptr2 = &y; ptr3 = &z; ptr3 = &z; printf (" %u %u %u\n", ptr1, ptr2, ptr3); printf (" %u %u %u\n", ptr1, ptr2, ptr3); *ptr3++; *ptr3++; printf (" %f %f %f\n", x, y, z); printf (" %f %f %f\n", x, y, z); printf (" %u %u %u\n", ptr1, ptr2, ptr3); printf (" %u %u %u\n", ptr1, ptr2, ptr3); printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); $./prec Systems Programming Pointers 19

Precedence Example (*ptr1)++; (*ptr1)++; printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); --*ptr2; --*ptr2; printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); printf (" %f %f %f\n", x, y, z); printf (" %f %f %f\n", x, y, z); return 0; return 0;} Systems Programming Pointers 20

Review of Pointer Basics   This section demonstrated the relationship between pointers and addresses and introduced the respective operators & and *.   Showed the use of pointers in simple examples.   Introduced call by reference with pointers.   Detailed the relationship between pointers and arrays. Systems Programming Pointers 21