17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
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
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
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!
Lectures on Numerical Methods1 Address of a variable zEach variable that is declared is stored in memory. Since memory is indexed each variable has an.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
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.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Pointers Applications
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.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
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.
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
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.
© 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.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
+ 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.
Arrays and Pointers.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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;
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Engineering Computing I Chapter 5 Pointers and Arrays.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
1 Pointers Chapter 07 CMPE-102 Introduction to Programming.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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
Windows Programming Lecture 03. Pointers and Arrays.
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.
Lecture 5 Pointers 1. Variable, memory location, address, value
CSE 220 – C Programming Pointers.
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Programming in C Pointer Basics.
Introduction to Problem Solving and Programming
Homework Starting K&R Chapter 5 Good tutorial on pointers
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur2 Values vs Locations zVariables name memory locations, which hold values. 32 x 1024: address name value New Type : Pointer A pointer contains a reference to another variable (its address)

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur3 Pointer 32 x 1024: int x; int * xp ; 1024 xp xp = &x ; address of x pointer to int *xp = 0;/* Assign 0 to x */ *xp = *xp + 1; /* Add 1 to x */ Pointers Abstractly int x; int *p; p=&x;... (x == *p) True (p == &x) True

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur4 Pointers zA variable in a program is stored in a certain number of bytes at a particular memory location, or address, in the machine. A pointer is the address of a variable in memory. zIf v is a variable, &v is its address. zDeclare p to be of type pointer to int. int * p;p = &i ; p = NULL;p = (int *) 1776; /* an absolute address in memory */

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur5 /*Declaring pointer variables*/ char * cp; /* pointer to char */ double * dp; /* pointer to double */ int * ip; /* pointer to int */ Operators for pointers The address operator & Generates the memory address of its operand (must be some- thing that is a memory object) int i = 4 ; int *ptr ; ptr = &i; The indirection operator * or dereferencing operator the value pointed to by -- if ptr = &i, *ptr and i mean the same thing. *ptr = *ptr + 1; /* i=5 */ ptr = ptr+1; /* modifies the memory address stored in ptr. */

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur6 Pointers zIf p is a pointer *p is the value of the variable of which p is the address. int a=1, b=2, *p; 1 a 2 b p p = &a; 12 abp b = *p ; 11 a b p

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur7 Call by value void swap1 (int x, int y) { int temp; temp = x; x = y; y = temp; } int main (void ) { int i = 3, j = 5; swap1 (i, j) ; printf (“%d,%d\n”,i,j); return 0; } 3,5 Program1 main 35 swap ij xy temp

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur8 Call by reference: output parameters void swap2 (int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } int main (void ) { int i = 3, j = 5; swap2(&i, &j) ; printf (“%d,%d\n”,i,j); return 0; } 5,3 Program2 main 35 ij swap pxpy 1024: 992:

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur9 Dissection of swap2 () zCall by value is used in C : whenever variables are passed as arguments to a function, their values are copied to the corresponding function parameters, and the variables themselves are not changed in the calling environment. zIn program 2, the addresses of the variables i and j (&i and &j) are passed to swap2. These addresses are copied to the pointer variables px and py respectively. px and py thus contain the addresses of i and j (local variables of main). Using px and py, the locations i and j can be accessed and their contents can be modified.

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur10 Call by reference zSome programming languages provide the call-by- reference mechanism that can be used to change the values of variables in the calling environment. C does not. zIn C, the effect of call-by-reference is achieved by : 1. Declaring a function parameter to be a pointer 2. Using the dereferenced pointer in the function body 3. Passing an address as an argument when the function is called.

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur11 Vocabulary zDereferencing or indirection : yfollowing a pointer to a memory location zOutput parameter: ya pointer parameter of a function ycan be used to provide a value (input), and/or store a changed value (output)

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur12 scanf demystified int x, y ; printf (“%d %d %d”, x, y, x+y) ; What about scanf ? scanf (“%d%d%d”,x,y,x+y); NO ! scanf (“%d%d”, &x, &y); YES !

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur13 Why use pointers ? zas output parameters: yfunctions that need to change their actual parameters. zto get multiple “return” values: yfunctions that need to return several results zto create dynamic data structures

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur14 Class Test 1 scripts zSection 5: 5 pm yRoom 107, CSE zSection 6 : 5 pm yRoom 108, CSE

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur15 Arrays and pointers zAn array name is an address, or a pointer value. zPointers as well as arrays can be subscripted. zA pointer variable can take different addresses as values. zAn array name is an address, or pointer, that is fixed.

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur16 Arrays and pointers int a[20], i, *p; zThe expression a[i] is equivalent to *(a+i) zp[i] is equivalent to *(p+i) zWhen an array is declared the compiler allocates a sufficient amount of contiguous space in memory. The base address of the array is the address of a[0]. zSuppose the system assigns 300 as the base address of a. a[0], a[1],...,a[19] are allocated 300, 304,..., 376.

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur17 Arrays and pointers #define N 20 int a[2N], i, *p, sum; zp = a; is equivalent to p = *a[0]; zp is assigned 300. zPointer arithmetic provides an alternative to array indexing. zp=a+1; is equivalent to p=&a[1]; (p is assigned 304) for (p=a; p<&a[N]; ++p) sum += *p ; p=a; for (i=0; i<N; ++i) sum += p[i] ; for (i=0; i<N; ++i) sum += *(a+i) ;

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur18 Arrays and pointers int a[N]; za is a constant pointer. za=p; ++a; a+=2; illegal

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur19 Pointer arithmetic and element size double * p, *q ; zThe expression p+1 yields the correct machine address for the next variable of that type. zOther valid pointer expressions: yp+i y++p yp+=i yp-q /* No of array elements between p and q */