Introduction to Problem Solving and Programming

Slides:



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

Programming and Data Structure
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.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
CSCI 171 Presentation 11 Pointers. Pointer Basics.
© 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!
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Pointers Applications
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
© 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.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Lecture 5 Pointers 1. Variable, memory location, address, value
Chapter 8 Arrays, Strings and Pointers
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
INC 161 , CPE 100 Computer Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
POINTERS.
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Pointers and References
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Pointers  Week 10.
Pointers and Pointer-Based Strings
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Pointers.
Pointers Kingdom of Saudi Arabia
Outline Defining and using Pointers Operations on pointers
5.1 Introduction Pointers Powerful, but difficult to master
Pointer Operations.
Pointers and Arrays Beyond Chapter 16
EENG212 ALGORITHMS & DATA STRUCTURES
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Pointers and dynamic objects
CS1100 Computational Engineering
Pointers and References
CISC181 Introduction to Computer Science Dr
Pointer Arithmetic By Anand George.
C Pointers Another ref:
Introduction to Pointers
Presentation transcript:

Introduction to Problem Solving and Programming Lecture 9 – Pointers

Addresses Each user-defined variable is stored at a location in the memory and has a value. The location is uniquely identified by an address. The address is retrieved by the “&” sign.

Pointers A pointer is a variable that “points” to the block of memory that another variable represents. In other words, it stores the memory address of another variable. The other variable may be of type int, char, array, structure, function or any other.

Variables Normal variables Pointer variables Contain a specific value (direct reference) Example : count Pointer variables Contain memory addresses as their values Example: countPtr   count 7 count 7 countPtr

Pointer Variable Declarations Pointer declarations Declaration: data_type *pointer_name; ‘*’ used with pointer variables int *myPtr; float *Ptr; Char *strPtr; Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2;

Pointer Variable Initialization Initialize pointers to: 0 or NULL, points to nothing (NULL preferred) Example: int *yPtr; an address of a variable points to an address of a user defined variable in the memory Example: int y = 5; int *yPtr; yPtr = &y; an address (absolute value) points to a certain place/address in the memory Example: int *ptr=(int *)1000;

Variables vs Pointer Variables int a=5; int * ptr; ptr=&a; About variable a: About variable ptr: 1. Name of variable : a 4. Name of variable: ptr 2. Value of variable: 5 5. Value of variable: 1025 3. Address: 1025 (assume) 6. Address: 5000 (assume)

Dereferencing = Using Addresses Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator. Given pointer ptr, to get value at that address, write *ptr after the declaration. Example: int x = 5; int *ptr = &x; *ptr = 6; /* Access x via ptr, and changes it to 6, equivalent to x = 6 */ printf(“%d”, x); // Will print 6 now

Example

Example (cont.) - output

Pointer Arithmetic Can do math on pointers Example: char* ptr; ptr+i = ptr + i * sizeof(data_type of ptr) ptr ptr + 1 a b c d

Data type sizes

Pointer Arithmetic Arithmetic operations can be performed on pointers Increment/decrement pointer (++ or --) Add an integer to a pointer ( + or += , - or -=) Pointers may be subtracted from each other Operations on a single pointer are meaningless unless performed on an array because it will access unrelated variables randomly.  

Pointer Expressions and Arithmetic Pointer comparison ( <, == , > ) See which pointer points to the higher numbered array element (index), subsequently higher address Also, checks if a pointer points to nothing

Pointer Arithmetic-Example #include<stdio.h> int main() { int *ptr=( int *)1000; ptr=ptr+1; printf(" %d",ptr); return 0; } Output 1004

Pointer Arithmetic-Example #include<stdio.h> int main() { double *p=(double *)1000; p=p+3; printf(" %d",p); return 0; } Output 1024

Pointer Arithmetic-Example #include<stdio.h> int main() { int *p=(int *)1000; int *temp; temp=p; p=p+2; printf("%d %d\n",temp,p); printf("difference= %d",p-temp); return 0; } Output 1000 1008 Difference= 2

Pointer Arithmetic-Example #include<stdio.h> int main() { float *p=(float *)1000; float *q=(float *)2000; printf("Difference= %d",q-p); return 0; } Output Difference= 250

Pointers & Arrays Array variables are pointers to the array start Example: char *ptr; char str[10]; ptr = str; //ptr now points to array start ptr = &str[0]; //Same as above line Array indexing is same as dereferencing after pointer addition. str[1] = ‘a’; *(ptr+1) = ‘a’;// same as above line

Pointers & Arrays  

a is a pointer only to the first element—not the whole array. Arrays and Pointers Note same a &a[0] a is a pointer only to the first element—not the whole array.

Arrays and Pointers  

Arrays and Pointers  

Arrays and Pointers Note To access an array, any pointer to the first element can be used instead of the name of the array.

Pointer Expressions (Recap) operation Description p++, p-- Increment (decrement) p to point to the next element, it is equivalent to p+=1 (p -=1) p+i , p-i Point to i-th element beyond (in front of) p but value of p is fixed p[i] Equivalent to p + i if p points to the start of array p + n (integer) n must be an integer, its meaning is offset p - q Offset between pointer p and pointer q p+q, p*q, p/q, p%q invalid Relational operator of two pointers p, q valid, including p > q, p < q, p == q, p >= q, p <= q, p != q  

Pointers & Arrays-Example #include <stdio.h> int main(void) { int i; int my_array[] = {1,23,17,4,-5,100}; //Compiler figures out size int *ptr; ptr = &my_array[0]; /* point our pointer to the first element of the array */ printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); } return 0;

Array of Pointers Arrays can contain pointers to …..

Array of Pointers int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; z[0] = a; // same as &a[0]; z[1] = a + 1; // same as &a[1]; z[2] = a + 2; // same as &a[2]; z[3] = a + 3; // same as &a[3]; for (int x=0;x<4;x++) printf("\n %d --- %d ",a[x],*z[x]);

for (int x=0;x<4;x++) printf("\n %d --- %d ",a[x], z[x]);

Array of Pointers Arrays can contain pointers to (array)

Array of Pointers int x[] = {32, 18, 12, 24}; int y[] = {13, 11, 16, 12, 42, 19, 14}; int *z[2] = {NULL, NULL}; int i; z[0] = x; // same as &x[0]; z[1] = y; // same as &y[0]; for (i=0;i<2;i++) printf("\n %d “,*z[i]);

Dynamic Allocation #include <stdlib.h> sizeof returns number of bytes of a data type. malloc finds a specified amount of free memory and returns a void pointer to it. ▫ char * str = (char *) malloc( 3 * sizeof(char) );

Dynamic DeAllocation #include <stdlib.h> free declares the memory pointed to by a pointer variable as free for future use: char * str = (char *) malloc( 3 *sizeof(char)); ... use str ... free(str);

Dynamically Allocated Arrays Allows you to avoid declaring array size at� declaration.� Use malloc to allocate memory for array when needed: Example: int *dynamic_array; dynamic_array = malloc( sizeof( int ) * 10 ); dynamic_array[0]=23; /* initialize value at the start of array to 23 */

Example

Questions?