Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Lectures 10 & 11.
Chapter 6 Data Types
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
1 CSE1301 Computer Programming Lecture 16 Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
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. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
© 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.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
System Programming Practical Session 7 C++ Memory Handling.
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.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
 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 Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
CSI-121 Structured Programming Language Lecture 16 Pointers
Computer Science 210 Computer Organization
Programming in C Pointer Basics.
Computer Skills2 for Scientific Colleges
Outline Defining and using Pointers Operations on pointers
Programming in C Pointer Basics.
Exercise Arrays.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Pointers Programming Applications

Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on either the run-time stack or on the heap. Pointers have names, values and types. Value of p versus value pointed to by p

Pointer  The definition of pointer is Int *p; float *p1; char *p2;  Which means that p is a pointer to integer data in memory and p1 is a pointer to floating point data in memory

Declaring pointer variables  it is not enough to say that a variable is a pointer. You also have to specify the type of variable to which the pointer points ! int * p1; // p1 points to an integer float * p2; // p2 points to a float  Exception: generic pointers (void *) indicate that the pointed data type is unknown may be used with explicit type cast to any type (type *) void * p; type * variable_name;

Pointer using  To use the pointer: int *p, k; K=10; P= &k;  Means that p has the address of k in memory  Then &k means the address of k and *p means the value in address p

Example main() { int v1=11, v2=22, *p; p=&v1; printf( “ %d ”, p); p=&v2; printf( “ %d ”, p); printf( “ %d ”, *p); }

Example main() { int v1=11, v2=22, *p; p=&v1; printf( “ %d ”, p); *p=33; printf( “ %d \n %d ”, p, *p); }

Example main() { int v1=11, v2, *p; p=&v1; printf( “ %d ”, p); v2=*p; printf( “ %d %d ”, p, v2); }

Pointer int u=3, v; int *pu, *pv; pu= &u; v= *pu; Pv=&v; Printf( “ %d %d %d %d ”, u, &u, pu, *pu); Printf( “ \n %d %d %d %d ”, v, &v, pv, *pv);

Pointer int u1, u2, v=3; int *pv; u1 = 2 * (v+5); pv = &v; u2 = 2 * (*pv +5); printf( “ %d %d ”, u1, u2);

Trace int x,y; int * px; x = 5; px = &x; printf("x = %d\n", x); printf("x = %d\n", *px); y = *px; printf("y = %d\n", y); *px = 10; printf("x = %d\n", x); printf("x = %d\n", *px); px = &y; *px = 15; printf("x = %d\n", x); printf("y = %d\n", y); printf("what = %d\n", *px);

Trace int x,z; float y; char ch, *chp; int *ip1, *ip2; float *fp; x = 100; y = 20.0; z = 50; ip1 = &x; ip2 = &z; fp = &y; chp = &ch; ip2 = ip1; ip1 = &z; *ip1 = *ip2; *ip1 = 200; *ip1 = *ip ; *fp = 1.2; printf("%f\n", *fp); printf("%d\n", ip1); printf("%d\n", *ip1);

Pointer and array  If we define the array int x[5]={2, 1, 5, 6, 8}  The variable x only means pointer to the first element of array x[]  If we define pointer int *p;  Then we can use p as p = &x[0] Which means that p is pointer to first element or p = x

Pointer and array  Both x and p can be used as printf( “ %d ”, *(x+2)); means the x[2] printf( “ %d ”, *(p+1)); means the x[1] printf( “ %d ”, (*x+2)); means the x[0]+2

Pointer Int x[8] = {10, 20, 30, 40, 50, 60, 70, 80,}; Printf( “ %d %d %d %d %d ”, x, x+2, *x, (*x+2), *(x+2));

Example float list[10], *p1; for(int i=0; i<10;i++, p1++) *p1=i; for( i=0; i<10;i++) printf( “ %f ”, *(pt-i));

Function and Pointer  We can call function by value f(s)  Or call function by reference f(&s)  Or define pointer p = &s and call function f(p)

Function and pointer  In the function call by value, any modification held in the function will not affect the original value  In call of reference, the modification in function affect the original value.

Example void set (int x); int main(void) {int x=10,y=11; set(x); set(y); printf( “ %d%d ”, x,y);} void set (int x){ int y=x+2; x*=2; }

Example void set (int *x); int main(void) {int x=10,y=11; set(&x); set(&y); printf( “ %d%d ”, x,y);} void set (int *x){ int y=*x+2; *x*=2; }

Example void set (int &); int main(void) {int x=10,y=11; set(&x); set(&y); printf( “ %d%d ”, x,y);} void set (int &x){ int y=x+2; x *=2; }

Passing array void cent (double *); void main(){ double varray[5]={10.0, 43.1, 95.9, 59.7, 87.3}; cent(varray); for(int j=0; j<5; j++) printf( “ %d ”, varray[j]);} void cent (double *pt){ for(int j=0; j<5; j++) *pt++ *= 2.54; }

Pointer & String void disp ( char *); void main() { char str[] = “ Idel people have the …” ; disp(str); } void disp (char *ps){ while (*ps != “ \0 ” ) printf( “ %c ”, *ps++); }

Array of string pointer We can define array of pointer to strings as: char * arrstr[7]={ “ Saturday ”, “ Sunday ”, “ Monday ”, “ Tuesday ”, “ Wednesday ”, “ Thursday ”, “ Friday ” } for (int j=0; j<7; j++) printf( “ %s ”, arrstr[j]);

Example float list[10] ={1, 5, 6, 9, 10, 0, 45, 4, 61, 7}; float *p, *d; P=list; d=&list[9]; for( i=0; i<10;i++) printf( “ %f ”, *(p+i),*(d-i) );

#include Void push(int *, int); Void pop (int *); Main() { int *p, value; For(int t=0; t<5;t++) {scanf( “ %d ”, &value); push(p, value);} Pop(p); pop(p); } Void push(int *p, int v){ p++; *p=v;} void pop (int *p){printf( “ %d ”, *(p--));}

Trace #include Int f (int); Void main() {Int y=f(0); Int z = f(4); Printf ( “ %d \t %d \n “, y, z); } int f(int x) {if (x==0) return 0; Else if (x>=1) return x+f(x-1); }

Example  Using pointer, write a program that read a list of numbers and sort them using swap function which accept two pointers to elements in this list and swap them.

Example  Write a program to read group of numbers from the user and then average them after stored them in an array and print the result. You should use pointer as possible.

Example  Write a function String which accept as argument the pointer to string and convert the string to all upper case. You can use the toupper() library function which takes a single character and return this character in upper case.

NULL pointers  Values of a pointer variable: Usually the value of a pointer variable is a pointer to some other variable Another value a pointer may have: it may be set to a null pointer  A null pointer is a special pointer value that is known not to point anywhere.  No other valid pointer, to any other variable, will ever compare equal to a null pointer !  Predefined constant NULL, defined in  Good practice: test for a null pointer before inspecting the value pointed ! #include int *ip = NULL; if(ip != NULL) printf("%d\n", *ip); if(ip ) printf("%d\n", *ip);

u Never use uninitialized pointers. u To increment a dereferenced pointer, use  (*p)++  rather than  *p++ /* correct but means … */

Generic Pointers and NULL Java: a reference to "any" kind of object  use a variable of type Object C: a reference to "any" kind of object  use a variable of type void*  void *p;  defines a generic, or typeless pointer p. Often casted to (T*)p NULL value can be assigned to any pointer, no matter what its type.

Generic Pointers and NULL  void *p = NULL;  int i = 2;  int *ip = &i;  p = ip;  printf("%d", *p);  printf("%d", *((int*)p ) );

Pointer Expressions and Pointer Arithmetic pv+n  pv + n*sizeof ( variable type that pointer point to )  Arithmetic operations can be performed on pointers Increment/decrement pointer ( ++ or -- ) Example: ++vPtr, vPtr++, --vPtr, vPtr-- Add an integer to a pointer( + or +=, - or -= ) Pointers may be subtracted from each other Operations meaningless unless performed on an array