{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C_lecture_3

# C_lecture_3 - CS 11 C track lecture 3 n This week n Arrays...

This preview shows pages 1–14. Sign up to view the full content.

CS 11 C track: lecture 3 This week: Arrays one-dimensional multidimensional Command-line arguments Assertions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Arrays What is an "array"? A way to collect together data of a single type in a single object A linear sequence of data objects e.g. array of int s array of char s (string)
Creating and using arrays One-dimensional array of three ints: int arr[3]; int sum; arr[0] = 1; arr[1] = 22; arr[2] = -35; sum = arr[0] + arr[1] + arr[2];

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
One-dimensional arrays (1) Arrays can be initialized partially initialized not initialized Uninitialized space contains? "garbage"
One-dimensional arrays (2) Examples: int my_array[10]; /* not initialized */ int my_array[5] = { 1, 2, 3, 4, 5 }; /* initialized */ int my_array[] = { 1, 2, 3, 4, 5 }; /* OK, initialized */ int my_array[4] = { 1, 2, 3, 4, 5 }; /* warning */ int my_array[10] = { 1, 2, 3, 4, 5 }; /* OK, partially initialized */

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
One-dimensional arrays (3) Note on partial initialization: int my_array[10] = { 1, 2, 3, 4, 5 }; rest of array initialized to 0 int my_array[10]; entire array uninitialized contains garbage
One-dimensional arrays (4) Explicit initialization of arrays: int i; int my_array[10]; for (i = 0; i < 10; i++) { my_array[i] = 2 * i; } This is the most flexible approach

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
One-dimensional arrays (5) Some bad things that can happen... int my_array[10]; /* What happens here? */ printf("%d\n", my_array[0]); /* What happens here? */ printf("%d\n", my_array[1000]); No checking! C is an UNSAFE language!
One-dimensional arrays (6) NOTE! The following is illegal: int my_array[5]; my_array = { 1, 2, 3, 4, 5 }; /* WRONG */ The { 1, 2, 3, 4, 5 } syntax is only usable when declaring a new variable, and not for reassigning the contents of the array int my_array[5] = { 1, 2, 3, 4, 5 }; /* OK */ int my_array[] = { 1, 2, 3, 4, 5 }; /* OK */

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Two-dimensional arrays (1) int arr[2][3]; /* NOT arr[2, 3] */ int i, j; int sum = 0; arr[0][0] = 1; arr[0][1] = 23; arr[0][2] = -12; arr[1][0] = 85; arr[1][1] = 46; arr[1][2] = 99; /* continued on next slide */
Two-dimensional arrays (2) for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { sum += arr[i][j]; } } printf("sum = %d\n", sum);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Two-dimensional arrays (3) Two-dimensional arrays can be split into component one-dimensional arrays: int arr[2][3]; /* initialize... */ /* arr[0] is array of 3 ints */ /* arr[1] is another array of 3 ints */
Two-dimensional arrays (5) How arr is laid out in memory: 1 23 -12 85 46 99 arr[0] arr[1]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}