lecture 2: arrays and pointers

29
Placement Preparation Arrays & Pointers Shobhit Chaurasia B.Tech, CSE

Upload: vivek-bhargav

Post on 25-May-2015

56 views

Category:

Engineering


2 download

DESCRIPTION

This slide is part of course designed for placement preparation for students of IIT Guwahati.

TRANSCRIPT

Page 1: Lecture 2: arrays and pointers

Placement Preparation

Arrays & Pointers

Shobhit ChaurasiaB.Tech, CSE

Page 2: Lecture 2: arrays and pointers

Target AudiencePeople who have no prior coding experience, didn’t take CS101 seriously but want to learn some basics of coding quickly.

If you know how write a program to sum up numbers in an array, and you DO NOT faint on seeing int ***ptr, then please don’t your waste time here; this tutorial is not for you.

Page 3: Lecture 2: arrays and pointers

Data types

● int : -1000, -5, 0, 10, 2014 ● char : a, @, +● float : -10.35, 2.578● bool : True/False

Page 4: Lecture 2: arrays and pointers

More Data types

● Array - {1,2,3,4}

● String - “Ghissu”● Pointers

Page 5: Lecture 2: arrays and pointers

Arrays

● Array is a collection of objects.

Page 6: Lecture 2: arrays and pointers

Arrays - 1D

● int test[10]; // space for 10 ints

● 0-based indexing

● Index from 0 to (size - 1)

Page 7: Lecture 2: arrays and pointers

Array - initialization

1D

2D

Page 8: Lecture 2: arrays and pointers

Demo #1

Store first 10 +ve integers in an array and print them.

Page 9: Lecture 2: arrays and pointers

Demo #2

Sum up all the numbers in an array.

Page 10: Lecture 2: arrays and pointers

Demo #3

Find the max. number in an array.

Page 11: Lecture 2: arrays and pointers

Demo #4

Reverse the contents of an array.

int arr[10]={1,2,3,4,5,6,7,8,9,10};

Page 12: Lecture 2: arrays and pointers

Arrays - 2D (Matrix)

● int test_score[x][y]

Row Index

Column Index

int test_score[10][4]

Page 13: Lecture 2: arrays and pointers

Demo #5

Output contents of a 2D int array.

Page 14: Lecture 2: arrays and pointers

Demo #6

char mat[6][3];

0 1 2

0 e o e

1 o e o

2 e o e

3 o e o

4 e o e

5 o e o

Page 15: Lecture 2: arrays and pointers

Arrays - 3D

Page 16: Lecture 2: arrays and pointers

Address of Variables

● int x = 10; //value of x is 10

● address of variable x is &x

10

x

0x7fff4d84231c

Variable Name

Data/Content

Address of variable

Page 17: Lecture 2: arrays and pointers

Demo #7

Print the value and address of a float

Page 18: Lecture 2: arrays and pointers

Pointers

Pointer is a variable that contains memory address.

ptr

0x7fff4d84231c

p

0x7fff01f8239c

f

0x00abcd12209

Page 19: Lecture 2: arrays and pointers

ptr

0x7fff4d84231c

p

0x243f01f8239a 0x00abcd12209

f

100x7fff4d84231c

s0x243f01f8239a

0.30x00abcd12209

Page 20: Lecture 2: arrays and pointers
Page 21: Lecture 2: arrays and pointers

Demo #8

&x returns the address of variable x

Page 22: Lecture 2: arrays and pointers

Pointer Dereferencing

int *ptr;ptr = &x;cout<<ptr;cout<<*ptr;

Pointer Declaration

Making ptr point to x (x MUST be int)

Will print address of x

Will print the value stored at x

60x7fff4d84231c

ptrx

Page 23: Lecture 2: arrays and pointers

Pointer Dereferencing

*ptr returns the value stored at the memory location pointed to by ptr

Page 24: Lecture 2: arrays and pointers

Demo #9➔ Show and explain 9.cpp➔ Ask them to reproduce the code without looking

Page 25: Lecture 2: arrays and pointers

NULL Pointer

int *ptr = NULL; //good coding practice

ptr

Page 26: Lecture 2: arrays and pointers

Pointer to Pointer

int x = 10;int *p = &x;int **q = &p;

0x32

q

0x74

0x66

p

0x32

10

x

0x66

Page 27: Lecture 2: arrays and pointers

Arrays as pointers

int arr[7];

arr

- 22 - 33 - 44 - 55 - 66 - 77 - 88

0014 0018 0022 0026 0030 0034 0038

0 1 2 3 4 5 6

arr + 1 arr +2 arr + 6

Address

Array Index

Data

Page 28: Lecture 2: arrays and pointers

- 22 - 33 - 44 - 55 - 66 - 77 - 88

0 1 2 3 4 5 6

0014 0018 0022 0026 0030 0034 0038

arr[0] is same as *(arr + 0) is same as -22&(arr[0]) is same as (arr + 0) is same as 0014

arr[1] is same as *(arr + 1) is same as -33&(arr[1]) is same as (arr + 1) is same as 0018

arr[5] is same as *(arr + 5) is same as -77&(arr[5]) is same as (arr + 5) is same as 0034

Array Index

int arr[7]

Address

arr

Page 29: Lecture 2: arrays and pointers

To be continued ...

● Dynamic memory allocation

● C-style strings.

● strcmp, strcat etc.