

































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































































Department of Computer Science
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int array[10]; 6 7 for (int i = 0; i < 10; i++) { 8 array[i] = i; 9 } 10 11 int n = 5; 12 int array2[n * 2]; 13 14 for (int i = 0; i < 10; i++) { 15 array2[i] = 3 * i - 2 * i * i; 16 } 17 18 printf("Size of array: %lu\n", sizeof(array)); 19 for (int i = 0; i < 10; ++i) { 20 printf("array[%i]=%+2i \t array2[%i]=%6i\n", i, array[i], i, array2[i]); 21 } 22 return 0; 23 }
Size of array: 40 array[0]=+0 array2[0]= 0 array[1]=+1 array2[1]= 1 array[2]=+2 array2[2]= - array[3]=+3 array2[3]= - array[4]=+4 array2[4]= - array[5]=+5 array2[5]= - array[6]=+6 array2[6]= - array[7]=+7 array2[7]= - array[8]=+8 array2[8]= - array[9]=+9 array2[9]= -
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int array[5] = {0, 1, 2, 3, 4}; 6 7 printf("Size of array: %lu\n", sizeof(array)); 8 for (int i = 0; i < 5; ++i) { 9 printf("Item[%i] = %i\n", i, array[i]); 10 } 11 return 0; 12 }
Size of array: 20 Item[0] = 0 Item[1] = 1 Item[2] = 2 Item[3] = 3 Item[4] = 4
double d[] = {0.1, 0.4, 0.5}; // initialization of the array
char str[] = "hallo"; // initialization with the text literal
char s[] = {’h’, ’a’, ’l’, ’l’, ’o’, ’\0’}; //elements
int m[3][3] = { { 1, 2, 3 }, { 4 , 5 ,6 }, { 7, 8, 9 }}; // 2D array
char cmd[][10] = { "start", "stop", "pause" };
void fce(int n) { // int local_array[n] = { 1, 2 }; initialization is not allowed int local_array[n]; // variable length array
printf("sizeof(local_array) = %lu\n", sizeof(local_array)); printf("length of array = %lu\n", sizeof(local_array) / sizeof(int)); for (int i = 0; i < n; ++i) { local_array[i] = i * i; } } int main(int argc, char *argv[]) { fce(argc); return 0; } lec04/fce_var_array.c
int m[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
printf("Size of m: %lu == %lu\n", sizeof(m), 33sizeof(int));
for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { printf("%3i", m[r][c]); } printf("\n"); }
Size of m: 36 == 36 1 2 3 4 5 6 7 8 9
int m[3][3] = { { 1, 2, 3 }, { 4, 5, 6}, { 7, 8, 9 } };
int *pm = (int *)m; // pointer to an allocated continuous memory block printf("m[0][0]=%i m[1][0]=%i\n", m[0][0], m[1][0]); // 1 4 printf("pm[0]=%i pm[3]=%i\n", m[0][0], m[1][0]); // 1 4
1 2 3 4 5 6 7 8 9
Row 0 Row 1 Row 2
(^) int **a; – pointer to pointer of the int value(s) (^) A pointer does not necessarily refer to a continuous memory E.g., in a case of pointer to array of pointers (^) Therefore, when accessing to a as to one-dimensional array int *b = (int *)a; the access to the second (and further) row cannot be guaranteed as in the above example
void print(int m[3][3]) { for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { printf("%4i", m[r][c]); } printf("\n"); } }
int m0[3][3]; int m1[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int m2[3][3] = { 1, 2, 3 }; int m3[3][3] = { [0][0] = 1, [1][1] = 2, [2][2] = 3 };
print(m0); print(m1); print(m2); print(m3);
m0 - not initialized
m1 - init by rows 1 2 3 4 5 6 7 8 9
m2 - partial init 1 2 3 0 0 0 0 0 0
m3 - indexed init 1 0 0 0 2 0 0 0 3