Lecture 2: arrays and pointers

Post on 25-May-2015

52 views 2 download

Tags:

description

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

Transcript of Lecture 2: arrays and pointers

Placement Preparation

Arrays & Pointers

Shobhit ChaurasiaB.Tech, CSE

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.

Data types

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

More Data types

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

● String - “Ghissu”● Pointers

Arrays

● Array is a collection of objects.

Arrays - 1D

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

● 0-based indexing

● Index from 0 to (size - 1)

Array - initialization

1D

2D

Demo #1

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

Demo #2

Sum up all the numbers in an array.

Demo #3

Find the max. number in an array.

Demo #4

Reverse the contents of an array.

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

Arrays - 2D (Matrix)

● int test_score[x][y]

Row Index

Column Index

int test_score[10][4]

Demo #5

Output contents of a 2D int array.

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

Arrays - 3D

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

Demo #7

Print the value and address of a float

Pointers

Pointer is a variable that contains memory address.

ptr

0x7fff4d84231c

p

0x7fff01f8239c

f

0x00abcd12209

ptr

0x7fff4d84231c

p

0x243f01f8239a 0x00abcd12209

f

100x7fff4d84231c

s0x243f01f8239a

0.30x00abcd12209

Demo #8

&x returns the address of variable x

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

Pointer Dereferencing

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

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

NULL Pointer

int *ptr = NULL; //good coding practice

ptr

Pointer to Pointer

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

0x32

q

0x74

0x66

p

0x32

10

x

0x66

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

- 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

To be continued ...

● Dynamic memory allocation

● C-style strings.

● strcmp, strcat etc.