1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
testing equality of floating point numbers
instead of x == y
you should write fabs(x-y)
, and you #define EPSILON
to a suitable value
explicit type casting
switch statements can only act on…
…ordinal values
general idiomatic form for switch case statements:
if you deliberately omit a break statement between cases, make it obvious by putting a comment
do while loop syntax
idiomatic form of writing an infinite loop
execution of code inside a loop (definitely a for loop, and perhaps do or while) can be manipulated by which statements
break: break out of the current loop
continue: jump to the end of the current loop (effectively ignoring everything below the continue statement)
enum syntax
enum day {MON, TUE, WED, THU, FRI, SAT, SUN};
when you initialise a value of this type, you must declare it as:
enum day d;
enum typedef example
typedef enum {MON, TUE, WED, THU, FRI, SAT, SUN} day;
we can now declare our variable as:
day d;
array declaration syntax
double data[MAXDATA];
to initialise an array at declaration:
int a[3] = {1, 2, 3}; // all elements specified
int b[10] = {1, 2, 3, 4}; // some elements unspecified
you may omit the array size when it can be inferred from the initializer:
int arr[] = {10,20,30,40,50,60};
how to find the number of elements in an array
#define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
iteration over an array
iteration over arrays only works when…
…the full declaration of the array is available. it doesn’t work when the array is passed as a parameter to a function - inside any function sizeof(arr)
delivers the size of a pointer, not the array.
so, when passing an array to a function, you usually pass a second parameter - the number of elements in the array.
strcpy()
char *
strncpy(char dst, const char src, size_t len);
copy the string src to dst (including the terminating ‘\0’ character
it’s your responsibility to ensure that there’s enough space for any string you strcpy() to fit, otherwise buffer overrun occurs
strcat()
char *
strcat(char restrict s1, const char restrict s2);
append a copy of the null-terminated string s2 to the end of the null-terminated string s1, then add a terminating ‘\0’
sprintf()
does formatted output into a string buffer, overwriting the existing contents
declare a pointer to an int variable e.g.
int *ip;
to declare and initialise:
int *ip = &x;
dereference operator
*
int *ip = &x; int y = *ip;
y is the dereferenced value of ip
how do you increment a pointer?
(*ip)++;
NOT
*ip++
this parses as *(ip++)
which means something entirely different
can you declare multiple pointers at once?
NO
use separate lines
all pointer arithmetic is…
scaled
pointer arithmetic: advancing a pointer within an array
as pointe arithmetic is scaled, ip++
is scaled by sizeof(*ip)
, here it adds sizeof(int)==4
to ip
idiomatic pointer-based strlen(s)
is pointer arithmetic allowed when you have a Pto1?
no, it is a classic error to do any pointer arithmetic when we’re dealing with a Pto1. pointer arithmetic (p++ etc) is only allowed when we’re dealing with a PtoM
pointer arithmetic: incrementing a pointer by N elements
ip += N;
can you add two pointers together?
no, thats meaningless
making a PtoM point to &arr[N] where the array has N elements…
this is a pointer one element beyond the end of the array, it’s allowed because many loops naturally lead to this final value.
you’re not allowed to dereference this pointer.
pointer arithmetic: for both comparison and subtraction, it is Undefined Behaviour if…
…the pointers are not PtoMs that both point within the same array
meaning of *p++
*p++
parses as *(p++)
and is a combination of *p
and p++
. Called post-increment, this delivers the current value of *p
and then increments the value of p
as a side-effect
compact and idiomatic implementation of strcpy(dst, src)
which 3 other combined dereference-and-inc/dec operators does C provide?
what are C’s 3 file-handles pre-opened from which data can be read or written?
printf()
usage
“print-formatted, “a versatile way of printing text to stdout
e.g.
printf("An integer: %d, and a character %c.\n", 42, c);