140 likes | 198 Vues
Arrays, Strings, and Pointers. CSE 2451 Matt Boggus. 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 "); }. Arrays.
E N D
Arrays, Strings, and Pointers CSE 2451 Matt Boggus
Another function topic – default arguments intf1(int arg1, double arg2, char* name, char *opt); int f2(int arg1, double arg2, char* name) { return f1(arg1, arg2, name, "Some option"); }
Arrays • Store many values of the same type in adjacent memory locations • Declaration <type> <name> [<size>] • Ex: • intrgb[3]; • double percents[10]; • char name[20]; • int s = 4; inttrythis[s]; // not valid for most compilers • <size> elements with an index/subscript number from 0 to <size>-1
Indexing • Dijkstra’s argument for zero index • Address of an array is the same as its first element • min = address of min[0]
Initializing Arrays • Initialize entire array using values enclosed in braces • Ex: intmyArray[5] = {1,2,3,4,5}; intmyArray[5] = {1,2}; // valid intmyArray[5] = {1,2,3,4,5,6}; // invalid • Initialize individual array locations • Ex: intmyArray[2]; myArray[0]=14; myArray[1]=4; • Array location acts like a single variable • Ex: x = myArray[0]; myArray[1]=x+y;
Array vs. Pointer Array name is a pointer constant int a[10]; int b[10]; int *c; … c = &a[0]; // c points to a[0] c = a; // equivalent b = a; // invalid a = c; // invalid
Copy an array #include <stdio.h> int main() { intiMarks[4] = {78, 64, 66, 74}; intnewMarks[4]; inti,j; for(i=0; i<4; i++) newMarks[i]=iMarks[i]; for(j=0; j<4; j++) printf("%d\n", newMarks[j]); return 0; }
More syntax examples int array[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]
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
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
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;
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)
Multi-dimensional arrays • Declarations – [row][col] subscript order • intvalues [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