1 / 14

Arrays, Strings, and Pointers

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.

danton
Download Presentation

Arrays, Strings, and Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays, Strings, and Pointers CSE 2451 Matt Boggus

  2. 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"); }

  3. 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

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

  5. 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;

  6. 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

  7. 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; }

  8. Shallow and Deep copying

  9. 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]

  10. 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

  11. 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 NULL byte (i.e. the \0 character) • Static allocation – size of the array is determined at compile-time

  12. 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;

  13. 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)

  14. 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

More Related