[SPCA] Start arrays overview

This commit is contained in:
2026-01-05 18:10:12 +01:00
parent 2067bdb190
commit 80997f5c15
4 changed files with 19 additions and 0 deletions

View File

@@ -1,3 +1,15 @@
#include <stdint.h>
#include <stdio.h>
int main( int argc, char *argv[] ) { int main( int argc, char *argv[] ) {
int data[ 10 ]; // Initialize array of 10 integers
data[ 5 ] = 5; // element 5 is now 5
*data = 10; // element 0 is now 5
printf( "%d\n", data[ 0 ] ); // print element 0 (prints 10)
printf( "%d\n", *data ); // equivalent as above
printf( "%d\n", data[ 5 ] ); // print element 5 (prints 5)
printf( "%d\n", *( data + 5 ) ); // equivalent as above
int multidim[ 5 ][ 5 ]; // 2-dimensional array
// We can iterate over it using two for-loops
return 0; return 0;
} }

Binary file not shown.

View File

@@ -0,0 +1,7 @@
\newpage
\subsubsection{Arrays}
\lC\ compiler does not do any array bound checks! Thus, always check array bounds.
Unlike some other programming languages, arrays are \bi{not} dynamic length.
The below snippet includes already some pointer arithmetic tricks. The variable \texttt{data} is a pointer to the first element of the array.
\inputcodewithfilename{c}{code-examples/00_c/}{03_arrays.c}

Binary file not shown.