CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Similar documents
CSE 214 Computer Science II Java Classes and Information Hiding

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ Recursion

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ Functions

CSE 230 Computer Science II (Data Structure) Introduction

Chapter 9 Introduction to Arrays. Fundamentals of Java

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

CSE 214 Computer Science II Heaps and Priority Queues

CSE 230 Intermediate Programming in C and C++ Binary Tree

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CSE 214 Computer Science II Searching

Arrays and Pointers (part 1)

CSE 230 Intermediate Programming in C and C++

... Lecture 12. Pointers

Arrays and Pointers (part 1)

How to declare an array in C?

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Module 6: Array in C

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Matrix Multiplication

Matrix Multiplication

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Homework #3 CS2255 Fall 2012

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

CSC 211 Intermediate Programming. Pointers

Arrays and Pointers in C. Alan L. Cox

First of all, it is a variable, just like other variables you studied

Question 13 1: (Solution, p 4) Describe the inputs and outputs of a (1-way) demultiplexer, and how they relate.

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Programming for Electrical and Computer Engineers. Pointers and Arrays

CSE 214 Computer Science II Introduction to Tree

Goals of this Lecture

SYSC 2006 C Winter 2012

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

type arrayname [ size type arrayname [ size ] = { item ,, item size size

Data Structures and Algorithms(12)

[0569] p 0318 garbage

Pointers as Arguments

Arrays and Pointers in C & C++

ESC101N: Fundamentals of Computing End-sem st semester

Pointers. Pointers. Pointers (cont) CS 217

Memory, Data, & Addressing II CSE 351 Spring

Algorithms & Data Structures

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

DECLARAING AND INITIALIZING POINTERS

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Pointers and Arrays 1

Pointers. Pointer References

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

Arrays and Pointers (part 2) Be extra careful with pointers!

What have we learned about when we learned about function parameters? 1-1

CS101 Computer Programming and Utilization

Data and File Structures Laboratory

Pointers. Memory. void foo() { }//return

CS 33. Caches. CS33 Intro to Computer Systems XVIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Introduction to Scientific Computing and Problem Solving

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Lesson 7. Reading and Writing a.k.a. Input and Output

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

CSE 214 Computer Science II Stack

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Language comparison. C has pointers. Java has references. C++ has pointers and references

CA31-1K DIS. Pointers. TA: You Lu

THE AUSTRALIAN NATIONAL UNIVERSITY First Semester Examination June 2011 COMP4300/6430. Parallel Systems

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Arrays in C. By Mrs. Manisha Kuveskar.

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Multidimension array, array of strings

Multi-Dimensional arrays

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS201- Introduction to Programming Current Quizzes

CSE 214 Computer Science II Final Review II

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CS/ECE 250 Computer Architecture

Tema 6: Dynamic memory

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Experiment Objectives. 2. Preparation. 3. Tasks. 3.1 Task A: String to Integer Conversion

Fundamental of Programming (C)

CS201 Latest Solved MCQs

Transcription:

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/

Definition: Arrays A collection of elements of the same type stored contiguously in memory under one name can be of any data type, e.g., integer, long integer, float, double, character etc. even collection of arrays! Arrays of structure, union, pointer etc. are also allowed Advantages: For ease of access to any element of an array Passing a group of elements to a function Shebuti Rayana (CS, Stony Brook University) 2

Array Representation A sample one-dimensional integer array Conceptual Picture Actual Picture 2 5 1 7 3 10 [0] [1] [2] [3] [4] [5] A collection of integer type elements Each element is associated with a location index In C, array index starts from zero Memory Address 1000 2 1002 5 1004 1 1006 7 1008 3 1010 10 content Shebuti Rayana (CS, Stony Brook University) 3

Arrays: Declaration & Initialization Declaration: int A[6]; An array of 6 integers A[0], A[1], A[2],, A[6] If array is declared within a function it contains garbage, if not initialized If array is globally declared it contains zeros Initialization: int A[6] = {2,5,1,7,3,10}; First index is 0, and Last index is array size-1 Accessing array element at index i: A[i] Shebuti Rayana (CS, Stony Brook University) 4

Arrays: Characteristics The storage class of arrays may be automatic, external, or static, but not register If external or static arrays are not initialized they are by default initialized to zero If an array is declared without a size and is initialized to a series of values, it implicitly given the size of the number of initializers. int A[] = {2,5,1,7,3,10}; size of array A is 6 here Shebuti Rayana (CS, Stony Brook University) 5

Arrays: Characteristics (cont.) Character arrays: char c[] = { a, b, c, \0 }; Alternatively: char c[] = abc ; Null character, represents end of string These two representations are equivalent string is a sequence of characters that is treated as a single data item and terminated by null character '\0'. C does not support strings as a data type. A string is actually onedimensional array of characters in C. Shebuti Rayana (CS, Stony Brook University) 6

Array Usage: Example Sum all the elements of an array Shebuti Rayana (CS, Stony Brook University) 7

Errors in array usage 1. If i has a value outside the range [0,size-1], a run-time error will occur when A[i]is accessed. Overrunning the bounds of an array is a common programming error The effect of the error is system-dependent Often the value of some unrelated variable will be returned 2. If local array is used before initialization garbage value will be processed Shebuti Rayana (CS, Stony Brook University) 8

2-dimensional array A 2D 3-by-3 integer array 2D square array not always necessary to have equal number of columns and rows rows [0] Declaration: int A[3][3]; [0] [1] [2] columns 2 5 1 7 3 10 0 1 6 Initialization: int A[3][3] = {{2,5,1},{7,3,10},{0,1,6}}; Applications: Matrix representation, e.g, graph adjacency matrix Shebuti Rayana (CS, Stony Brook University) 9 [1] [2]

2D Array for Graph Adjacency Matrix int A[6][6] = {{0,1,1,0,0,0}, {1,0,0,1,0,0}, {1,0,0,1,1,0}, {0,1,1,0,0,1}, {0,0,1,0,0,1}, {0,0,0,1,1,0}}; 0 1 2 3 Undirected unweighted plain graph 4 5 Shebuti Rayana (CS, Stony Brook University) 10

2D Arrays in Memory In the computer memory, all elements are stored linearly using contiguous addresses. In order to store a two-dimensional matrix, two dimensional address space must be mapped to one-dimensional address space. In the computer's memory matrices are stored in either Row-major order or Columnmajor order form. Shebuti Rayana (CS, Stony Brook University) 11

2D Arrays in Memory (cont.) Conceptual Picture [0] [1] [2] [0] 2 5 1 [1] 7 3 10 [2] 0 1 6 Actual Picture Address Content Index 1000 2 (0, 0) 1002 5 (0, 1) 1004 1 (0, 2) 1006 7 (1, 0) 1008 3 (1, 1) 1010 10 (1, 2) 1012 0 (2, 0) 1014 1 (2, 1) 1016 6 (2, 2) Row Major Order Example is given for row major order only Shebuti Rayana (CS, Stony Brook University) 12

2D Array Usage: Example Matrix multiplication code for matrix a and b int i, j, k; for (i = 0; i < n; i++) { } for (j = 0; j < n; j++) { } double sum = 0; for (k = 0; k < n; k++) { } sum += a[i][k] * b[k][j]; c[i][j] = sum; Shebuti Rayana (CS, Stony Brook University) 13

Pointers Shebuti Rayana (CS, Stony Brook University) 14

Introduction A variable in a program is stored in a certain number of bytes at a particular memory location or address. Pointers are used to access memory and manipulate address. If v is a variable, then &v gives its memory address Address operator & is an unary operator Shebuti Rayana (CS, Stony Brook University) 15

Pointers: Declaration Example Declaration: int *p; p is a pointer to integer The indirection or dereferencing operator * is unary Its range of values include a special address 0 and a set of positive integers that represent machine addresses. Example assignment to pointer p p = 0; p = Null; // same as p = 0 p = &i; // pointing to i p = (int *)1776; /* absolute address */ Shebuti Rayana (CS, Stony Brook University) 16

Pointers: Characteristics If p is a pointer then *p is the value of the variable of which p is the address. Direct value of p is an address of a memory location, and *p is indirect value of p, which is the value stored in that memory location. In a certain sense * is the inverse operator of & Shebuti Rayana (CS, Stony Brook University) 17

Pointers: Example int a = 1, b = 2, *p; a 1 Think of the pointer as an arrow, but it is not yet assigned a value. So, we do not know what it points to Next line: p = &a a 1 b = *p; b =? b 2 b 2 p p Shebuti Rayana (CS, Stony Brook University) 18

Pointers: Example Code Value of i: 7 Location of i: effffb24 A pointer can be initialized in a declaration. The variable p is of type int and its initial value is &i. The declaration of i must occur before we take its address. Shebuti Rayana (CS, Stony Brook University) 19

Pointers: Declaration and Initialization Declaration and Initialization int i=3,j=5,*p=&i,*q=&j,*r; double x; Expression Equivalent Value Expression p == &i p == (&i) 1 **&p *(*(&p)) 3 r = &x r = (&x) illegal 7* *p/ *q+7 ((7*(*p))/(*q ))+7 11 *(r=&j) *= *p (*(r = (&j))) *= (*p) 15 Shebuti Rayana (CS, Stony Brook University) 20

Constructs not to be pointed at Do not point at constants. &3 /* illegal */ Do not point at ordinary expressions. &(k + 99) /* illegal */ Do not point at register variables. register v; &v /* illegal */ Address operator can be applied to variables and array elements. If a is an array, expressions such as &a[0] and &a[i+j+3] make sense. Shebuti Rayana (CS, Stony Brook University) 21

Call-by-reference "call-by-reference" is a way of passing addresses (references) of variables to a function that then allows the body of the function to make changes to the values of variables in the calling environment. Call by value Output: i = 5 j = 10 Output: i = 10 j = 5 Call by reference Shebuti Rayana (CS, Stony Brook University) 22

Relationship between Arrays and Pointers A pointer variable can take different addresses as values. In contrast, an array name is an address, or pointer, that is fixed. So following are illegal: a = p ++a a += 2 &a Suppose a is an array and i is an int, a[i] is equivalent to *(a+i) Equivalent expressions: #define N 100 int a[n], i, *p, sum = 0; p = a equivalent to p = &a[0] p = a + 1 equivalent to p = &a[1] Shebuti Rayana (CS, Stony Brook University) 23

Relationship between Arrays and Pointers Following 3 for loops are equivalent: for(p = a; p < &a[n]; ++p) sum += *p; for(i = 0; i < N; ++i) sum += *(a+i); p=a; for(i = 0; i < N; ++i) sum += p[i]; Shebuti Rayana (CS, Stony Brook University) 24