Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.

Similar presentations


Presentation on theme: "Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int."— Presentation transcript:

1 Arrays, Strings, and Pointers CSE 2451 Rong Shi

2 Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int rgb[3]; – double percents[10]; – char name[20]; – int s = 4; int trythis[s]; // not valid for most compilers elements with an index/subscript number from 0 to -1

3 Indexing Dijkstra’s argument for zero index Address of an array is the same as its first element – min = address of min[0]

4 Initializing Arrays Initialize entire array using values enclosed in braces – Ex: int myArray[5] = {1,2,3,4,5}; int myArray[5] = {1,2};// valid int myArray[5] = {1,2,3,4,5,6};// invalid Initialize individual array locations – Ex: int myArray[2]; myArray[0]=14; myArray[1]=4; Array location acts like a single variable – Ex: x = myArray[0]; myArray[1]=x+y;

5 Array vs. Pointer Array name is a pointer constant inta[10]; intb[10]; int*c; … c = &a[0];// c points to a[0] c = a;// equivalent b = a;// invalid a = c;// invalid

6 Copy an array #include int main() { int iMarks[4] = {78, 64, 66, 74}; int newMarks[4]; int i,j; for(i=0; i<4; i++) newMarks[i]=iMarks[i]; for(j=0; j<4; j++) printf("%d\n", newMarks[j]); return 0; }

7 Shallow and Deep copying

8 More syntax examples intarray[10]; int*ap = array + 2; // ap points at &array[2] 1. ap 2.*ap 3.ap[0] 4.ap + 6 5.*ap + 6 6.*(ap + 6) 7.ap[6] 8.&ap 9.ap[-1] 10.ap[9]

9 Index checking Index access is not checked by the compiler – Check for valid range manually – Especially important for user entered indices int array[2] = {0,0}; int input; scanf(“%d”, &input); array[input];// what could possibly go wrong? Attempt to access memory outside of range allocated by OS – Segmentation fault – Access OS memory

10 Character arrays – strings Character array terminated with the null byte Declaration – char arr[] = {'c','o','d','e','\0'}; – char arr[] = "code"; Array size is not specified in [] Logical size of the array is one more than the number of characters in the string literal – one extra for NUL byte (i.e. the \0 character) Static allocation – size of the array is determined at compile-time

11 Pointers and Strings In C our string type is an array of characters Array names are pointer constants Use the same syntax char *ptr; char str[40]; ptr = str;

12 Arrays as function parameters Prototypes: intstrlen( char *string ); intstrlen( char string[] ); Logically, a pointer to the first element is passed copy-by-value (See array operations examples)

13 Multi-dimensional arrays Declarations – [row][col] subscript order int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } – Values is an array of 3 elements, each an array of 4 elements Stored in row order

14 Backup

15 Another function topic – default arguments int f1(int arg1, double arg2, char* name, char *opt); int f2(int arg1, double arg2, char* name) { return f1(arg1, arg2, name, "Some option"); }


Download ppt "Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int."

Similar presentations


Ads by Google