ESc101: Pointers and Arrays

Similar documents
ESC101N Fundamentals of Computing

Programming Language B

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

ESC101N: Fundamentals of Computing End-sem st semester

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Principles of C and Memory Management

Chapter 11: Pointers

Memory Management. CSC215 Lecture

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

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

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

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

Subject: Fundamental of Computer Programming 2068

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

3/13/2012. ESc101: Introduction to Computers and Programming Languages

Arrays and Pointers (part 1)

Arrays and Pointers (part 1)

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

Pointers Part 1. Introduction

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:

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Fundamental of Programming (C)

PDS Class Test 2. Room Sections No of students

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

CSCI 2132 Software Development. Lecture 19: Generating Permutations

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

Computer Programming Unit 3

20 Dynamic allocation of memory: malloc and calloc

Dynamic memory allocation (malloc)

Pointers as Arguments

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

Lecture06: Pointers 4/1/2013

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Pointers. Introduction

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Dynamic Data Structures. CSCI 112: Programming in C

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

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

Class Information ANNOUCEMENTS

Content. In this chapter, you will learn:

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C - Func(on, Pointer, Array. Zhaoguo Wang

Arrays, Strings, and Pointers

CSCI 2132 Software Development. Lecture 18: Functions

Arrays, Strings, and Pointers

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

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

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

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

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

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

o Code, executable, and process o Main memory vs. virtual memory

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

C Programming Language

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Dynamic Memory Allocation

Variables, Pointers, and Arrays

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Decimal Representation

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Introduction to C. Systems Programming Concepts

1d: tests knowing about bitwise fields and union/struct differences.

Assist. Prof. Dr. Caner ÖZCAN

Laboratory 10 POINTERS I. FUNDAMENTALS

Two Dimensional Arrays

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

Dynamic Memory Allocation

Arrays and Pointers in C. Alan L. Cox

MODULE 5: Pointers, Preprocessor Directives and Data Structures

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

BSM540 Basics of C Language

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Quick review pointer basics (KR ch )

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

Computer Science & Engineering 150A Problem Solving Using Computers

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

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

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

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming with Indexed Variables

EM108 Software Development for Engineers

Structures and Pointers

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

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

2.2 Pointers. Department of CSE

M.CS201 Programming language

Array Initialization

int marks[10]; // fixed size and fixed address No change in Memory address.

MODULE V: POINTERS & PREPROCESSORS

C PROGRAMMING Lecture 5. 1st semester

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Transcription:

ESc101: Pointers and Arrays Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 The content of some of these slides are from the lecture slides of Prof. Arnab Bhattacharya 2 1

Movie Theater Seat Allocation Vs. Variable Allocation Ticket 1 E2; Memory size ~ theater seat capacity variable declaration ~ seat number allocation variable address ~ seat number variable value ~ name of person in that seat Ticket 1: Aditya ; 3 Pointers to variables Movie Theater Seat Allocation Buy Ticket 1 Seating agent seats the person in the seat E2 Ticket 1 Seat number = E2 name of person having Ticket 1 is Aditya Bharat is asked to sit in E2 person with Ticket 1 is now Bharat Variable Allocation char A; scanf( %c, &A); /*the input character is stored in address 5*/ char *pc; pc=&a; //pc=5 Value of A is G ; *pc = V ; /* A = V now */ 4 2

Swapping contents of pointers in functions Function: pointer content swap void swap(int *pa, int *pb) int temp; temp = *pb; *pb = *pa; *pa = temp; return; } printf( a=%d b=%d, a, b); swap (&a, &b); printf( a=%d b=%d, a, b); /*a and b would now be swapped*/ Theater: swap persons in seats swapping the persons seated in seats of Ticket 1 and Ticket 2 E.g. Aditya in seat E2 having Ticket 1 is swapped with Bharat in seat D5 having Ticket 2. Person having Ticket 1 is now Bharat. Person having Ticket 2 is Aditya. 5 Theater Seat Allocation Vs. Array Allocation Aditya Bharat Chaitanya Dilip Buy 4 tickets for Music group G1-G4 G4 allotted to Music group Seat 4 persons from G1 to G4 size of Music group = 4 Music group starts at G1 and starting Music group member is Aditya int Music[4]; Music allotted 4 integer spaces from address 5000 for (i=0; i<4; i++) scanf( %d,&music[i]); //Input: 6 5 8 7 sizeof(music) = 4*sizeof (int)=4*4b=16b // Music points to the first location // *Music = 6 // *(Music+i) = Music[i] 6 3

Theater Seat Allocation Vs. Array Allocation array declaration ~ allocation of seat numbers to a group array size ~ number of seats array element value/content t ~ name of person in that t seat array name ~ seat number of the first person in the group The array name is a constant pointer to the address of the first element int a[5] = 5, 4, 3, 2, 1}; a is the address of a[0] (hence a pointer) a is a constant pointer, as it is fixed to the address of a[0] a[0] is the value of the first element of the matrix, i.e. 5. *a is the same as a[0], and is the value of the first element 7 Dynamic memory allocation Dynamic memory allocation is required when the programmer cannot determine in advance how much space will be required by the program Space is dynamically allocated using malloc() malloc() takes size in bytes as a parameter and returns void *, i.e., a pointer without a specific type Explicit type casting of this pointer is required Space should be freed after use using free() free() takes as input a pointer returned by malloc() 8 4

Dynamic memory allocation to arrays Array allocation int n, *Music; Music = (int *) malloc(n *sizeof(int)); for (i = 0; i < n; i++) Music[i] = i; free(music); /*space freed*/ Theater seating Allot agent to hold seats Give n seats for Music group Music group member 0 to n-1 are Aditya, Bharat, Music group leaves the seats 9 Dynamic double array #include <stdio.h> #include <stdlib.h> // required for malloc int main() double *a; int i, n; double b[7] = 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0}; printf("enter the size of array: "); scanf("%d", &n); a = (double *)malloc(n * sizeof(double)); /* sizeof(double) is required as it is in bytes*/ printf("size of a is %d\n", sizeof(a)); // size of the pointer printf("size of b is %d\n", sizeof(b)); /*size of array is the total space allotted in bytes*/ printf("number of elements in b is %d\n", sizeof(b) / sizeof(double)); 10 5

Dynamic double array (cont.) for (i = 0; i < n; i++) a[i] = i; // array notation for (i = 0; i < n; i++) printf("%lf %lf\t", a[i], *(a + i)); printf("\n"); printf("&a is %u, while a[0] is at %u\n", &a, &a[0]); variable stored elsewhere*/ printf("&b is %u, while b[0] is at %u\n", &b, &b[0]); separately*/ /*a is a separate /* b is not stored } free(a); // important as otherwise space is not freed 11 One dimensional array allocation 700 b = 500 &b[0] = 500 b[0] = 10.0 n 400 i 404 b[0] 500 508 516 524 532 540 548 a 600 3 12 0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 5500 a[0] 5500 5508 5516 After array allocation, a = 5500 &a[0] = 5500 After array assignment, a[0] = 0.0 0.0 1.0 2.0 12 6

One dimensional array access int num[5]; num: address of num[0] num: pointer to num[0] num[0] num[k] &num[k] int *num, n=5; num = (int *) malloc(n*sizeof(int) ); *num *(num+k) num+k 13 2D arrays are stored contiguously 0 d[0][0] 500 d[0][1] 508 d[0][2] 516 d[1][0] 524 d[1][1] 532 d[1][2] 540 600.. 10.0 9.0 8.0 7.0 6.0 5.0 2D arrays are stored rowwise double d[2][3] = 10.0,9.0, 090 8.0, 7.0, 6.0, 5.0}; d = 500, i.e. address of array d d[0] = 500, i.e. address of d[0][0] d[0][0] = 10.0 d[1] = 524, i.e. pointer to d[1][0] d[1][0] = 7.0 14 7

Arrays of pointers Since a pointer is a variable, arrays of pointers can be declared The declaration char *a[3]; declares a to be an array of 3 pointers to char a[i] is a pointer to char Common way to declare arrays of strings instead of char a[3][30]; Useful since the strings can be of variable size char *a[3] = `` Kolkata '', `` Kanpur '', `` Hyderabad '' }; The array pointed to by each a[i] is allotted space through dynamic 15 memory allocation Array of pointers to characters #include <stdio.h> #include <stdlib.h> int main() char *a[3]; int i, n; printf("enter maximum length of string %d: ", i); scanf("%d", &n); a[i] = (char *)malloc(n * sizeof(char)); // allocate space for each a[i] } 16 8

Array of pointers to characters (cont.) printf("enter string %d: ", i); scanf("%s", a[i]); } printf("%s\n", a[i]); printf("size of a is %d\n", sizeof(a)); //size of 3 pointer variables=3*8b=24b printf("size of a[%d] is %d\n", i, sizeof(a[i])); //size of ith char array printf("a is at %u\n", &a); printf("a[%d] is at %u\n", i, &a[i]); 17 Array of pointers to characters (cont.) printf("a[%d][0] is at %u\n", i, &a[i][0]); } free(a[i]); // free each a[i] 18 9

Array of pointers to characters i 400 n 404 a[0] 500 a[1] 508 a[2] 516 a[0][0] ] 600 601 602 603 604 012 45 a[1][0] 600 800 900 a[2][0] S I T A \0 700.. 800 801 802 803 900 901 902 903 904 905 R A M \0 K U S H \0 19 Two dimensional array access int num[5][3]; num: address of num[0] num[j]: address of num[j][0] num[0][0] num[j][k] num[j] &(num[j][k]) Can access through pointers int **num, n=5, m=3; num = (int **) malloc(n*sizeof(int *) ); num+j = (int *) malloc (m*sizeof(int) ); *(*(num)) *(*(num+j)+k) *(num+j) *(num+j)+k Can access through array notation. e.g. num[j][k] 20 10