lec26-cs110 computational engineering
DESCRIPTION
A keynote on Problem Solving using ComputersTRANSCRIPT
Pointers
V. Ka makoti
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)
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));
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
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
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