Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34…… 173172174175176177178179180181.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Introduction to C Programming
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Chapter 7: Arrays In this chapter, you will learn about
Spring Semester 2013 Lecture 5
An introduction to pointers in c
Lectures 10 & 11.
Pointers and Arrays Chapter 12
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Arrays and Strings.
One Dimensional Arrays
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
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.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
1 More on Pointers. 2 Reminder 3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Pointers CSE 2451 Rong Shi.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Review 1 List Data Structure List operations List Implementation Array Linked List.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Pointers.
Basic notes on pointers in C
Pointers and References
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Exercise Arrays.
Presentation transcript:

Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34…… …… P

Referencing The unary operator & gives the address of a variable The statement P=&C assigns the address of C to the variable P, and now P points to C To print a pointer, use %p format.

Referencing int C; int *P; /* Declare P as a pointer to int */ C = 7; P = &C; C 7 34…… …… P

Dereferencing The unary operator * is the dereferencing operator Applied on pointers Access the object the pointer points to The statement *P=5; Puts in C (the variable pointed by P) the value 5

Dereferencing printf(“%d”, *P); /* Prints out ‘7’ */ *P = 177; printf(“%d”, C); /* Prints out ‘177’ */ P = 177; /* This is unadvisable! */ C 7 34…… …… P

Example pointers.c

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2] …

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); xy zip Z[0]Z[1]Z[2]

Common errors It is impossible to define pointers to constants or expressions. It is also impossible to change a variable’s address (because it is not for us to determine!). Therefore, the following are errors: i = &3; j = &(k+5); k = &(a==b); &a = &b; &a = 150;

Pass arguments by value The functions we saw till now accepted their arguments “by value” They could manipulate the passed values They couldn’t change values in the calling function

Wrong Swap val_swap.c

How can we fix it? We can define swap so it gets pointers to integers instead of integers void swap(int *x, int *y) { …swap *x and *y… } We then call swap by swap(&x,&y); This is passing values by address

Right Swap add_swap.c

Back to scanf We can now understand the & in scanf(“%d”,&a); The argument list in scanf is simply passed by address, so scanf can change its content

Exercise Write a function that accepts a double parameter and returns its integer and fraction parts. Write a program that accepts a number from the user and prints out its integer and fraction parts, using this function

Solution dbl_split.c

Exercise The relation between rectangular and polar coordinates is given by – r = sqrt(x 2 +y 2 ) θ = tan -1 (y/x) Implement a function that accepts two rectangular coordinates and returns the corresponding polar coordinates Use the function atan defined in math.h

Solution rec_to_polar.c

Pointers and Arrays Recall that an array S holds the address of its first element S[0] S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0]

Pointer-array equivalence Arrays are actually a kind of pointers! When an array is defined, a fixed amount of memory the size of the array is allocated. The array variable is set to point to the beginning of that memory segment When a pointer is declared, it is uninitialized (like a regular variable) Unlike pointers, the value of an array variable cannot be changed

Passing arrays to functions Arrays can be passed to functions and have their values changed. This is possible because an array variable is actually an address. The two following function argument declarations are equivalent – int arr[] int *arr Example – vec_mul.c

Arrays as function arguments Functions can accept arrays as arguments Usually the array’s size also needs to be passed (why?) For example - int CalcSum(int arr[], int size); Within the function, arr is accessed in the usual way Example – calc_sum.c

Exercise Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise Write a program that accepts two arrays of integers from the user and checks for equality

Solution compare_arrays.c

Exercise Implement the function int Subsequence(int arr1[], int size1, int arr2[], int size2); The function returns 1 iff arr2 is a subsequence of arr1 For example, if arr1 = {1, 4, 6, 8} and arr2 = {4, 6}.

Solution subsequence.c

Exercise Write a program that accepts positive integers from the user until a negative one is entered The program then displays the 5 largest integers entered Not necessarily sorted by size! Note – there’s no need to define functions other than main

Solution top_numbers.c