int a = 0;
size_t a_size = sizeof(a++);
printf("size: %zu, a: %d\n", a_size, a);
#define swap(a, b) temp = a; \
a = b; \
b = temp;
void selection_sort(int* a, size_t len){
size_t temp = len - 1;
for(size_t i = 0; i < temp; ++i){
size_t min_index = i;
for(size_t j = i+1; j < len; ++i){
if(a[j] < a[i]) min_index = j;
}
if(i != min_index)
swap(a[i], a[min_index]);
}
}
short mystery_bits(short input){
short max_set = ~0;
short masked = input & (0xFF00 ^ max_set);
short shifted = masked << 16;
short ret = (shifted | 0xCC);
return ret;
}
void positive_under_ten(int input){
if(0 < input < 10){
printf("Input is in the range\n");
}else{
printf("Input is not in the range\n");
}
}
int print_error(int err_num){
switch(err_num){
case ENOENT:
printf("No such file or entry\n");
case EINTR:
printf("Interrupted\n");
default:
break;
}
}
Where can a pointer point to?
Heap
Stack
Global
Text
…Anywhere!
A pointer that stores the address of a function’s code
With this, we can pass a function as an argument
This allows us to reuse code
E.g., sorting an array of arbitrary objects could use a function pointer as the comparison function
You will be debugging several functions that all use pointers incorrectly
-
We’ve given you a set of functions as tools
Your job is to use these tools in the right order to print “Illinois”
Maybe, we don’t want to pass around the size of the array everywhere
We know that NULL is not a valid string (but this doesn’t work for every type)
char *ptr[] = ...;// vs
int arr[] = ...;
arr[len] = NULL; // NULL = 0, meaning we have a valid element
strdup
: return a string copystrcpy
,strncpy
: copy a string to another stringtoupper
, tolower
: uppercases/lowercases an input characterispunct
,isspace
,isalpha
: decide whether a character is punctuation/alphabetical/whitespace
strtok