mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-11 01:18:27 +00:00
[SPCA] Finish arrays, strings, some fixes
This commit is contained in:
@@ -11,5 +11,10 @@ int main( int argc, char *argv[] ) {
|
||||
printf( "%d\n", *( data + 5 ) ); // equivalent as above
|
||||
int multidim[ 5 ][ 5 ]; // 2-dimensional array
|
||||
// We can iterate over it using two for-loops
|
||||
int init_array[ 2 ][ 2 ] = {
|
||||
{1, 2},
|
||||
{3, 4}
|
||||
}; // We can initialize an array like this
|
||||
int empty_arr[ 4 ] = {}; // Initialized to 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
15
semester3/spca/code-examples/00_c/04_strings.c
Normal file
15
semester3/spca/code-examples/00_c/04_strings.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
char hello[ 6 ] = "hello"; // Using double quotes
|
||||
char world[ 6 ] = { 'w', 'o', 'r', 'l', 'd', '\0' }; // As array
|
||||
|
||||
char src[ 12 ], dest[ 12 ];
|
||||
strncpy( src, "ETHZ", 12 ); // Copy strings (extra elements will be set to \0)
|
||||
strncpy( dest, src, 12 ); // Copy strings (last arg is first n chars to copy)
|
||||
if ( strncmp( src, dest, 12 ) ) // Compare two strings. Returns 1 if src > dest
|
||||
printf( "Hello World" );
|
||||
strncat( dest, " is in ZH", 12 ); // Concatenate strings
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
\subsubsection{Strings}
|
||||
\lC\ doesn't have a \texttt{string} data type, but rather, strings are represented (when using \texttt{ASCII}) as \texttt{char} arrays,
|
||||
with length of the array $n + 1$ (where $n$ is the number of characters of the string).
|
||||
The extra element is the termination character, called the \texttt{null character}, denoted \verb|\0|.
|
||||
To determine the actual length of the string (as it may be padded), we can use \verb|strnlen(str, maxlen)| from \texttt{string.h}
|
||||
\inputcodewithfilename{c}{code-examples/00_c/}{04_strings.c}
|
||||
|
||||
0
semester3/spca/parts/00_c/01_basics/06_integers.tex
Normal file
0
semester3/spca/parts/00_c/01_basics/06_integers.tex
Normal file
0
semester3/spca/parts/00_c/01_basics/07_pointers.tex
Normal file
0
semester3/spca/parts/00_c/01_basics/07_pointers.tex
Normal file
@@ -0,0 +1 @@
|
||||
\subsection{The C preprocessor}
|
||||
|
||||
0
semester3/spca/parts/00_c/03_memory/00_intro.tex
Normal file
0
semester3/spca/parts/00_c/03_memory/00_intro.tex
Normal file
0
semester3/spca/parts/00_c/05_toolchain/00_intro.tex
Normal file
0
semester3/spca/parts/00_c/05_toolchain/00_intro.tex
Normal file
Binary file not shown.
@@ -67,6 +67,7 @@
|
||||
\input{parts/00_c/01_basics/04_arrays.tex}
|
||||
\input{parts/00_c/01_basics/05_strings.tex}
|
||||
\input{parts/00_c/01_basics/06_pointers.tex}
|
||||
\input{parts/00_c/02_preprocessor/00_intro.tex}
|
||||
|
||||
|
||||
% ── Intro to x86 asm ────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user