INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Intro to Pointers in C CSSE 332 Operating Systems
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
Functions and Pointers
Standard Version of Starting Out with C++, 4th Edition
POINTERS.
Chapter 9: Pointers.
EPSII 59:006 Spring 2004.
Pointers in C.
Pointers and Pointer-Based Strings
Pointers.
Student Book An Introduction
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Chapter 10: Pointers 1.
Chapter 9: Pointers.
Chapter 9: Pointers.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
INC 161 , CPE 100 Computer Programming
Computer Skills2 for Scientific Colleges
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
Pointers.
Initializing variables
Objectives You should be able to describe: Addresses and Pointers
Pointers and Pointer-Based Strings
Exercise Arrays.
Standard Version of Starting Out with C++, 4th Edition
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Pointers.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 11 Pointer (part 1)

What is a pointer? Memory 2000 Pointers are variables that store address of memory where data is stored. Pointers value are in hexadecimal. 2001 2002 2003 2004 2005 2006 2007 e.g. A pointer “P” has a value of 0x2008 which means it points to this address 2008 2009 200A 200B

How to Define Pointer Variables Use an asterisk * in front of a variable name. For example, int *p; p stores address of an integer or p points to an integer Note: We need to specify the type of data that the pointer points to. p address

Two Symbols of Pointer Operators & (address operator) To obtain the address of a variable, add the address operator ‘&’ in front of the variable name. int i = 10; int *iPtr; iPtr = &i; //iPtr gets address of i iPtr <==> &i * (indirection/dereferencing operator) To declare a variable of pointer, add the asterisk ‘*’ in front of the variable name. To obtain the value of a variable, add the indirection operator ‘*’ in front of a pointer’s variable name. *iPtr <==> i *iptr gives the value of i (iptr points to i) * can be used for assignment *iptr = 5; // changes i to 5

&i , p &p Address EC7 Address F8E p i i , *p int i, *p; i = 3; p = &i;

Example: } #include <stdio.h> main() { int i, j, *p; /* declare i and j as int, p as pointer to int */ i = 10; j = 0; p = &i; /* p is assigned with the address of i. p points to i, *p <==> i */ *p = 20; /* The memory pointed by p is assigned with 20 */ j = *p; /* j is assigned with the value pointed by p */ }

A sample memory layout for two integers and one pointer For the statement p = &i, if the address of i is 0x00E81E20, the value of p is 0x00E81E20 as shown below For before and after indirection operations *p = 20; j = *p;

More About Pointer Variable Declarations Pointer has a value of 0 or NULL means it points to nothing. 0 or NULL pointer points to nothing (NULL preferred) Pointer to void, (void *) Generic pointer, represents any type. void pointers cannot be dereferenced void *p; /* *p is invalid */

The Size of Pointer Variables The variable of pointer type is used to hold an address of the memory. The size of a variable of pointer type is implementation-dependent. It is typically 4 bytes for 32-bit machines and 8 bytes for 64-bit machines. It is independent of the variable type such as char or double that it points to.

Pointer Arithmetic Following arithmetic operations can be performed on pointers Add an integer to a pointer ( p+i or p+=i) If p is a pointer to some element of an array, then p+i (or p+=i) increments it to point to ith element beyond where it currently points to. For example, if p points to the 4th element of an integral array a with 6 elements which has address of 0x008C9550, p = &a[3]; p+2 (or p+=2) points to 0x008C955C+2*sizeof(int)=0x008C9564 Subtract an integer from a pointer (p-i or p-=i) p-2 (or p-=2) points to 0x008C955C-2*sizeof(int)=0x008C9554

Pointer Arithmetic (Cont.) Increment/decrement pointer (++ or --) If p is a pointer to some element of an array, then ++p (p+1) increments p to point to the next element. -- p (p-1) decrements to the previous element. For example, if p points to the 4th element of an integral array a which has address of 0x008C9550, p = &a[3]; ++p points to 0x008C955C+sizeof(int)=0x008C9560. -- p points to 0x008C955C-sizeof(int)=0x008C9558.

Pointer Arithmetic (Cont.) > int a[6]={100,200,300,400,500,600}, *p > p = &a[0] // p = 0x008C9550, p pointers to a[0] > *p 100 > p += 3 // p = 0x008C955C, p pointers to a[3] 400 > *(p+1) 500 > *(p-1) 300 > *p+1 401 > *p = 11 // set a[3] = 11 > *(p+2) = 12 // set a[5] = 12 > *(p-2) = 13 // set a[1] = 13 The contents of array a after assignment statements.

Passing values into/out of Functions Using Pointers Pointers can be used to pass the addresses of variables to functions and access variables through their addresses indirectly. Through this method, it is possible to write functions to alter arguments passed into them.

Program 1: Write a program to swap the values of a and b. Output: /* File: swap.c */ #include <stdio.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } main() { int a = 5, b = 6; swap(a, b); printf("a = %d, b = %d\n", a, b); // a = 5, b = 6 a = 5, b = 6 Note: Program 1 would not be able to swap the values of a and b, but only the values of x and y.

Before swap operations After swap operations

To pass a value in and out of a function, Pass a pointer to the variables instead. void function(int *xx) { : } main() { int x; function(&x) Function definition (accept pointer) Declaration (regular variable) Usage: pass pointer

Output: Note: Program 2 would be able to swap the values of a and b, #include <stdio.h> void swap(int *pa, int *pb) { int temp; temp = *pa; *pa = *pb; *pb = temp; } main() { int a = 5, b = 6; swap(&a, &b); printf("a = %d, b = %d\n", a, b); // a = 6, b = 5 Output: a = 6, b = 5 Note: Program 2 would be able to swap the values of a and b, and yield a=6 and b=5.

Before swap operations After swap operations *pa <==> a *pb <==> b

Example: Function scanf() uses pointers to pass results through its arguments. /* File: scanfc.c */ #include <stdio.h> main() { int num; printf("Please enter an integer number:\n"); scanf("%d", &num); // pass a pointer to scanf() printf(“Your input number is %d\n", num); }

Relationship Between Pointers and Arrays Arrays and pointers are closely related Array name acts as a constant pointer. Its value is the address of the first element of the array. So p = a; is equivalent to p = &a[0];. Pointers can do array subscripting operations. Assume an integral array a has 6 elements. The 3rd element of array a can be accessed by int a[6] = {1,2,3,4,5,6}; a[2] *(a+2) int *p; p = a; or p = &a[0]; (3) *(p+2) (4) p[2]

Example: The sample code below illustrates four methods to access elements of an array: #include <stdio.h> main() { int i, *p; int a[10] = {0}; a[3] = 3; for(i=0; i<10; i++) printf("%d ",a[i]); printf("\n"); *(a+4) = 4; p = a; *(p+5) = 5; p[6] = 6; }

Relation of multi-dimensional array and pointer Example: main() { int b[3][4] = {0}; int *p; p = &b[1][1]; p[0] = 1; *(p+2) = 2; p[3] = 3; } A pointer can point to 2-dimensional array, but can do only 1-level subscript.