I'm reading a book on coding an operating system and I'm writing the C code examples they have in the book and compiling and testing the code in the terminal but I ran into a problem with this code.
The file with this code in it is named "hello.c".
I compiled the file with "gcc hello.c -o hello
"
and then ran it with "./hello
".
I received the message Segmentation fault (core dumped), and I'm unsure of what I'm doing wrong.
#include <stdio.h>
void preinit1() {
printf("%s\n", __FUNCTION__);
}
void preinit2() {
printf("%s\n", __FUNCTION__);
}
void init1() {
printf("%s\n", __FUNCTION__);
}
void init2() {
printf("%s\n", __FUNCTION__);
}
typedef void (*preinit)();
typedef void (*init)();
__attribute__((section(".init_array"))) preinit
preinit_arr[2] = {preinit1, preinit2};
__attribute__((section(".init_array"))) init
init_arr[2] = {init1, init2};
int main(int argc, char *argv[])
{
printf("hello world!\n");
return 0;
}