lec26-cs110 computational engineering

6

Click here to load reader

Upload: sriharsha-p

Post on 22-May-2015

26 views

Category:

Education


0 download

DESCRIPTION

A keynote on Problem Solving using Computers

TRANSCRIPT

Page 1: Lec26-CS110 Computational Engineering

Pointers

V. Ka makoti

Page 2: Lec26-CS110 Computational Engineering

Pointers and MultidimensionalArrays

• A two dimensional array is a collection ofCONTIGUOUS one dimensional arrays.

• int x[10][20] - can be written as– int (*x)[20];– “x” points to the first element of the first 20

element array - first (zeroth) row– “x+1” points to the first element of the second 20

elements array - One’th row.– Interestingly - x[2][5] is equivalent to *(*(x+2) + 5)

Page 3: Lec26-CS110 Computational Engineering

Contiguous Arrays continued

• a[row][col] == *(*(a+row) + col)• scanf (“%d”,&a[row][col]);• scanf(“%d”,(*(a+row)+col));• printf (“%d”,a[row][col]);• printf(“%d”,*(*(a+row)+col));

Page 4: Lec26-CS110 Computational Engineering

Strings and Pointers

• char *name - is a string• char *names[] - is an array of strings• char names[10][12]

– Array of 10 strings, each 12 characters inlength.

• char *names[10];– names[0], ….., names[9] are all pointers– So array of pointers

Page 5: Lec26-CS110 Computational Engineering

Strings and Pointers

• If we want an array of “10” strings each ofmaximum size “n” characters

• char *names[10];– names[0], ….., names[9] are all pointers– So array of pointers– Different from 2-dimensional arrays– 2-dimensional arrays are CONTIGUOUS one

dimensional arrays• Array of strings NEED NOT BE

CONTIGUOUS ONE DIMENSIONAL Arrays

Page 6: Lec26-CS110 Computational Engineering

Objective

• Understand difference between 2-Darrays and Array of Pointers.

• Declaration• int x[10][20]; int (*x)[20]• char mystr[10][20]; char *mystr[10]• What is char (*mystr)[20] - a two

dimensional array of characters