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

Similar documents
Data and File Structures Laboratory

SYSC 2006 C Winter 2012

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Understanding Pointers

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

[0569] p 0318 garbage

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Computers Programming Course 10. Iulian Năstac

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

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

Fall 2018 Discussion 2: September 3, 2018

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

Assist. Prof. Dr. Caner ÖZCAN

In Java we have the keyword null, which is the value of an uninitialized reference type

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

Dynamic Data Structures. CSCI 112: Programming in C

CS201- Introduction to Programming Current Quizzes

Lab 3. Pointers Programming Lab (Using C) XU Silei

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Dynamic Memory Allocation

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Fundamental of Programming (C)

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

Computer Organization & Systems Exam I Example Questions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Pointers as Arguments

Dynamic memory allocation

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

BBM 201 DATA STRUCTURES

Data Representation and Storage

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

Procedural programming with C

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

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

Dynamic memory allocation (malloc)

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

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Memory Management. CSC215 Lecture

C Tutorial Pointers, Dynamic Memory allocation, Makefile

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

C Structures & Dynamic Memory Management

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS453 Arrays in Java in general

CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Welcome! COMP s1. Programming Fundamentals

Lecture06: Pointers 4/1/2013

Memory Allocation. General Questions

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Complex data structures. Cedric Saule

Programming for Engineers Arrays

Pointers, Arrays, and Strings. CS449 Spring 2016

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Answer all questions. Write your answers only in the space provided. Full marks = 50

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

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

Array Initialization

Arrays and Pointers (part 1)

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

CS61C : Machine Structures

Arrays and Pointers in C. Alan L. Cox

20 Dynamic allocation of memory: malloc and calloc

Pointers (continued), arrays and strings

dynamic memory allocation

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Pointers. Introduction

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Applications of Arrays (1A) Young Won Lim 3/15/17

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 4 (week 5) Section 4.10 Pointer and Dynamic Memory Allocation. CS 50 Hathairat Rattanasook

Module 6: Array in C

Character Strings. String-copy Example

Laboratory 10 POINTERS I. FUNDAMENTALS

Arrays, Pointers and Memory Management

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Content. In this chapter, you will learn:

Pointers (continued), arrays and strings

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

CSC 270 Survey of Programming Languages. What is a Pointer?

Dynamic Allocation of Memory Space

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

Problem Solving and 'C' Programming

Transcription:

Arrays in C Data and File Structures Laboratory http://wwwisicalacin/~dfslab/2017/indexhtml DFS Lab (ISI) Arrays in C 1 / 1

What is an array? A A[0] A[1] A[2] A[n 1] Syntax Sequence of n contiguous memory locations Length of the array = n Elements of the array each of the n memory locations Elements numbered 0 through n 1 char chararray[128], c; int intarray[64], i, j; chararray[i] = c; // 0 <= i <= 127 intarray[0] = i; j = intarray[63]; DFS Lab (ISI) Arrays in C 2 / 1

Strings Definition Strings are character arrays, but the end of the string is marked by the first occurrence of \0 in the array (not the last element of the array) Example: char str1[16]; str1[0] = a ; str1[1] = b ; str1[2] = c ; str1[3] = \0 ; // NOT 0 /* str1 now holds the string "abc" */ a b c \0 end of the string end of the array DFS Lab (ISI) Arrays in C 3 / 1

Pointers Memory = consecutively numbered storage cells (bytes) Variable can occupy one or more bytes Address of a variable = serial number of first byte occupied by the variable Pointer holds the address of a variable x p DFS Lab (ISI) Arrays in C 4 / 1

Operations & address / location operator * dereferencing operator char c, *cp; int i, *ip; cp = &c; ip = &i; *cp = 0; /* same as c = 0 */ DFS Lab (ISI) Arrays in C 5 / 1

Operations & address / location operator * dereferencing operator char c, *cp; int i, *ip; cp = &c; ip = &i; *cp = 0; /* same as c = 0 */ ip + n points to n-th object after what ip is pointing to ip - n points to n-th object before what ip is pointing to DFS Lab (ISI) Arrays in C 5 / 1

Pointers and arrays An array name is synonymous with the address of its first element Conversely, a pointer can be regarded as an array of elements starting from wherever it is pointing DFS Lab (ISI) Arrays in C 6 / 1

Pointers and arrays An array name is synonymous with the address of its first element Conversely, a pointer can be regarded as an array of elements starting from wherever it is pointing int a[10] = {}, *p; p = a; /* same as p = &(a[0]); */ *p = 5; /* same as a[0] = 5; */ p[2] = 6; /* same as a[2] = 6; */ *(a+3) = 7; /* same as a[3] = 7; */ DFS Lab (ISI) Arrays in C 6 / 1

Pointers and arrays An array name is synonymous with the address of its first element Conversely, a pointer can be regarded as an array of elements starting from wherever it is pointing int a[10] = {}, *p; p = a; /* same as p = &(a[0]); */ *p = 5; /* same as a[0] = 5; */ p[2] = 6; /* same as a[2] = 6; */ *(a+3) = 7; /* same as a[3] = 7; */ But: p = a; p++; RIGHT a = p; a++; WRONG DFS Lab (ISI) Arrays in C 6 / 1

Allocating memory Syntax: (type *) malloc(n * sizeof(type)) (type *) calloc(n, sizeof(type)) (type *) realloc(ptr, n * sizeof(type)) free(ptr) DFS Lab (ISI) Arrays in C 7 / 1

Allocating memory Syntax: (type *) malloc(n * sizeof(type)) (type *) calloc(n, sizeof(type)) (type *) realloc(ptr, n * sizeof(type)) free(ptr) Convenient macros: #define Malloc(n,type) (type *) malloc( (unsigned) ((n)*sizeof(type))) #define Realloc(loc,n,type) (type *) realloc( (char *)(loc), \ (unsigned) ((n)*sizeof(type))) DFS Lab (ISI) Arrays in C 7 / 1

Multi-dimensional arrays Multi-dimensional array = array of arrays = pointer to pointer int **a, i; a = (int **) malloc(rows * sizeof(int *)); for (i = 0; i < rows; i++) a[i] = (int *) malloc(cols * sizeof(int)); a a[0] a[1] a[rows-1] DFS Lab (ISI) Arrays in C 8 / 1

Multi-dimensional arrays rows mat temp cols int ii; int *temp; if (NULL == (temp = (int *) malloc(rows*cols*sizeof(int))) NULL == (mat = (int **) malloc(rows * sizeof(int *)))) ERR_MESG("Out of memory"); for (ii = 0; ii < rows; temp += cols, ii++) mat[ii] = temp; DFS Lab (ISI) Arrays in C 9 / 1

Review questions 1 Suppose s and t are strings What does the following do? while (*s++ = *t++); 2 What output is generated by the following code? for (i=0; i < 10; i++) printf("abcdefghijklmnop\n" + i); DFS Lab (ISI) Arrays in C 10 / 1

Review questions Solutions 1 String copying do { *s = *t; s++; t++; } while (*t!= \0 ); DFS Lab (ISI) Arrays in C 11 / 1

Review questions Solutions 1 String copying do { *s = *t; s++; t++; } while (*t!= \0 ); do { *s++ = *t++; } while (*t!= \0 ); DFS Lab (ISI) Arrays in C 11 / 1

Review questions Solutions 1 String copying do { *s = *t; s++; t++; } while (*t!= \0 ); do { *s++ = *t++; } while (*t!= \0 ); while ((*s++ = *t++)!= \0 ); DFS Lab (ISI) Arrays in C 11 / 1

Review questions Solutions 2 Think of the problem this way: p = "abcdefghijklmnop\n"; printf(p); DFS Lab (ISI) Arrays in C 12 / 1

Review questions Solutions 2 Think of the problem this way: p = "abcdefghijklmnop\n"; printf(p); p = "abcdefghijklmnop\n"; printf(p + 2); DFS Lab (ISI) Arrays in C 12 / 1

Review questions Solutions 2 Think of the problem this way: p = "abcdefghijklmnop\n"; printf(p); p = "abcdefghijklmnop\n"; printf(p + 2); p = "abcdefghijklmnop\n"; printf(p + i); DFS Lab (ISI) Arrays in C 12 / 1

Programming problems - Day 2 1 Consider 2 sequences of integers, A and B, stored in arrays (a) Write a program to find the number of (possibly overlapping) occurrences of the sequence B in A (b) Write a program to find whether the multisets corresponding to A and B are equal 2 Write a program to find the highest and second highest value in an array of n integers using less than 2n 3 comparisons DFS Lab (ISI) Arrays in C 13 / 1