This lesson goes over pointers, addresses, bit fields, dynamic memory allocation, linked lists, hash maps, hash tables, dereferencing pointers, comparing pointers, and pointers to functions.
This program illustrates the difference between a data type’s struct without bit fields, and a struct with them.
typedef struct {
unsigned short month;
unsigned short day;
unsigned short year;
} DateBig;
typedef struct {
unsigned short month : 4;
unsigned short day : 5;
unsigned short year;
} DateSmall;This program shows how to dereference pointers, and shows the difference between a pointer and what it points to.
This program show how to use a pointer to a function, call it, and pass it as an argument.
void eachColor(void (*fp)(Color *c)) {
int i;
for (i = 0; i < MAX; i++) {
// this runs the function pointed to by the first argument to this function
(*fp)(&Colors[i]);
}
}This is a working example of a hash map.
typedef struct {
char *key;
int value;
} item;
item *linear_search(item *items, size_t size, const char *key) {
for (size_t i = 0; i < size; i++) {
if (strcmp(items[i].key, key) == 0) {
return &items[i];
}
}
return NULL;
}
int main(void) {
item items[] = {
{"one", 10},
{"two", 20},
{"three", 30},
{"four", 40},
{"five", 50}
};This program show how to create a hash table/associative array using linked lists.
typedef struct list_t {
char *key;
char *value;
struct list_t *next;
} list;
typedef struct hashTable_t {
unsigned int size;
list **array;
/* each cell of this array is a pointer to the first node of a
* linked list */
} hashTable;Linked lists with push and pop functions. A linked list is a dynamically-allocated array of data objects, each containing a pointer to the next object in the sequence.
typedef struct node {
int val;
struct node *next;
} node_t;
void print_list(node_t *h) {
node_t *current = h;
while (current != NULL) {
printf("%x = %d\n", ¤t->val, current->val); // print address of value and value
current = current->next; // advance to next item
}
}Simple malloc() example, can allocate up to 20 characters.
char *p;
const int len = 20;
p = (char *)malloc(len * sizeof(char));
if (p == NULL) {
fprintf(stderr, "Insufficient memory\n");
return 1;
}
else {
printf("Enter a string: ");
fgets(p, len, stdin);
printf("Address of string: %p\n", (void *)&p);
printf("The string is: %s", p);
free(p);
p = NULL;
}
return 0;True dynamic memory allocation with realloc(), has no size limit on strings it can allocate.
Small example that shows how to dereference pointers to structs/objects.
typedef struct {
char name[70];
int age;
double gpa;
} Student;
void getStudent(Student *s) {
printf("Enter name: ");
scanf(" %[^\n]", s->name);
printf("Enter age: ");
scanf("%d", &s->age);
printf("Enter GPA: ");
scanf("%lf", &s->gpa);
}Example that shows how to use a union.