Given the following C code:
#include <stdio.h>
#include <string.h>
typedef struct a {
char name[10];
} a;
typedef struct b {
char name[10];
} b;
void copy(a *source, b *destination) {
strcpy(destination->name, source->name);
}
This main function below runs successfully:
int main() {
a first;
b second;
strcpy(first.name, "hello");
copy(&first, &second);
printf("%s\n", second.name);
printf("Finished\n");
return 1;
}
While this main function results in a segmentation fault:
int main() {
a *first;
b *second;
strcpy(first->name, "hello");
copy(first, second);
printf("%s\n", second->name);
printf("Finished\n");
return 1;
}
From my understanding of C, both implementations should run identically. What are the differences between the implementations and how can I adjust the second implementation to successfully run to completion?