1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.

Slides:



Advertisements
Similar presentations
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……
Advertisements

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
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.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
1 Pointers. Variable Memory Snapshot 2 int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient.
Kernighan/Ritchie: Kelley/Pohl:
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
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!
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
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 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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Pointers CSE 2451 Rong Shi.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
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;
Engineering Computing I Chapter 5 Pointers and Arrays.
UNIT 8 Pointers.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Lecture 5 Pointers 1. Variable, memory location, address, value
CSE 220 – C Programming Pointers.
EPSII 59:006 Spring 2004.
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
POINTERS.
Pointer.
INC 161 , CPE 100 Computer Programming
Basic notes on pointers in C
Pointers and References
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Topics discussed in this section:
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
Pointers and Pointer-Based Strings
Exercise Arrays.
Arrays and Pointers CSE 2031 Fall May 2019.
C Pointers Systems Programming.
Arrays and Pointers CSE 2031 Fall July 2019.
FUNCTION ||.
Presentation transcript:

1 Pointers ( מצביעים )

2 Variables in memory Primitives Arrays

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 34…… …… P

4 Brief Summary of today’s session… &x – address (pointer) of variable x *x – content in address x (common) usage: int x = *y; (common) usage: printf(“%d”,*y); int */double */char * - define pointer to the corresponding primitive (common) usage: int * x = &y; int ** ?

5 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.

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

7 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

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

9 Example pointers.c

10 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] …

11 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]

12 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]

13 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]

14 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]

15 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]

16 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]

17 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]

18 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]

19 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]

20 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]

21 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]

22 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;

23 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

24 Wrong swap (val_swap.c) void swap(int x, int y) { int temp; temp=x; x=y; y=temp; } int main(void) { int x=1,y=2; printf("before swap: x=%d, y=%d\n",x,y); swap(x,y); printf("after swap: x=%d, y=%d\n",x,y); }

25 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

26 Right Swap (add_swap.c) void swap(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } int main(void) { int x=1,y=2; printf("before swap: x=%d, y=%d\n",x,y); swap(&x,&y); printf("after swap: x=%d, y=%d\n",x,y); }

27 Insights 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 Other relevant examples from the past? Can we now “return” more then a single value from a function? How?

28 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

29 Solution dbl_split.c

30 home 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

31 Solution rec_to_polar.c

32 Exercise Implement a function that accepts an integer array, and finds the two numbers that are closest together For example, if the array is – {1, 5, 7, 10, 6, 19} The function should find 5 and 6 (or 6 and 7, it doesn’t matter)

33 Solution array.c

34 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]

35 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