Why am I getting an "undefined reference to `parseAndExecute' " error here? - c

I'm using an online tutorial on how to make a Text Adventure game (in VSC) and came across a really annoying issue. Even after copying all the source code from the tutorial, I receive an 'undefined reference' error. I tried editing the 'tasks.json' file as described here: undefined reference error in VScode
but with no success.
The game consists of multiple c files in order to work, the source code is at the bottom of the page here: https://helderman.github.io/htpataic/htpataic05.html
Here's all the files, Thanks a lot in advance for any help!
<main.c>
#include <stdbool.h>
#include <stdio.h>
#include "parsexec.h"
static char input[100] = "look around";
static bool getInput(void)
{
printf("\n--> ");
return fgets(input, sizeof input, stdin) != NULL;
}
int main()
{
printf("Welcome to Little Cave Adventure.\n");
while (parseAndExecute(input) && getInput());
printf("\nBye!\n");
return 0;
}
<parsexec.h>
extern bool parseAndExecute(char *input);
<parsexec.c>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "location.h"
#include "inventory.h"
bool parseAndExecute(char *input)
{
char *verb = strtok(input, " \n");
char *noun = strtok(NULL, " \n");
if (verb != NULL)
{
if (strcmp(verb, "quit") == 0)
{
return false;
}
else if (strcmp(verb, "look") == 0)
{
executeLook(noun);
}
else if (strcmp(verb, "go") == 0)
{
executeGo(noun);
}
else if (strcmp(verb, "get") == 0)
{
executeGet(noun);
}
else if (strcmp(verb, "drop") == 0)
{
executeDrop(noun);
}
else if (strcmp(verb, "give") == 0)
{
executeGive(noun);
}
else if (strcmp(verb, "ask") == 0)
{
executeAsk(noun);
}
else if (strcmp(verb, "inventory") == 0)
{
executeInventory();
}
else
{
printf("I don't know how to '%s'.\n", verb);
}
}
return true;
}
<location.c>
#include <stdio.h>
#include <string.h>
#include "object.h"
#include "misc.h"
#include "noun.h"
void executeLook(const char *noun)
{
if (noun != NULL && strcmp(noun, "around") == 0)
{
printf("You are in %s.\n", player->location->description);
listObjectsAtLocation(player->location);
}
else
{
printf("I don't understand what you want to see.\n");
}
}
void executeGo(const char *noun)
{
OBJECT *obj = getVisible("where you want to go", noun);
if (obj == NULL)
{
// already handled by getVisible
}
else if (obj->location == NULL && obj != player->location)
{
printf("OK.\n");
player->location = obj;
executeLook("around");
}
else
{
printf("You can't get much closer than this.\n");
}
}
<location.h>
extern void executeLook(const char *noun);
extern void executeGo(const char *noun);
<object.c>
#include <stdio.h>
#include "object.h"
OBJECT objs[] = {
{"an open field", "field" , NULL },
{"a little cave", "cave" , NULL },
{"a silver coin", "silver" , field },
{"a gold coin" , "gold" , cave },
{"a burly guard", "guard" , field },
{"yourself" , "yourself", field }
};
<object.h>
typedef struct object {
const char *description;
const char *tag;
struct object *location;
} OBJECT;
extern OBJECT objs[];
#define field (objs + 0)
#define cave (objs + 1)
#define silver (objs + 2)
#define gold (objs + 3)
#define guard (objs + 4)
#define player (objs + 5)
#define endOfObjs (objs + 6)
<noun.c>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "object.h"
static bool objectHasTag(OBJECT *obj, const char *noun)
{
return noun != NULL && *noun != '\0' && strcmp(noun, obj->tag) == 0;
}
static OBJECT *getObject(const char *noun)
{
OBJECT *obj, *res = NULL;
for (obj = objs; obj < endOfObjs; obj++)
{
if (objectHasTag(obj, noun))
{
res = obj;
}
}
return res;
}
OBJECT *getVisible(const char *intention, const char *noun)
{
OBJECT *obj = getObject(noun);
if (obj == NULL)
{
printf("I don't understand %s.\n", intention);
}
else if (!(obj == player ||
obj == player->location ||
obj->location == player ||
obj->location == player->location ||
obj->location == NULL ||
obj->location->location == player ||
obj->location->location == player->location))
{
printf("You don't see any %s here.\n", noun);
obj = NULL;
}
return obj;
}
OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun)
{
OBJECT *obj = NULL;
if (from == NULL)
{
printf("I don't understand who you want to %s.\n", verb);
}
else if ((obj = getObject(noun)) == NULL)
{
printf("I don't understand what you want to %s.\n", verb);
}
else if (obj == from)
{
printf("You should not be doing that to %s.\n", obj->description);
obj = NULL;
}
else if (obj->location != from)
{
if (from == player)
{
printf("You are not holding any %s.\n", noun);
}
else
{
printf("There appears to be no %s you can get from %s.\n",
noun, from->description);
}
obj = NULL;
}
return obj;
}
<noun.h>
extern OBJECT *getVisible(const char *intention, const char *noun);
extern OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun);
<inventory.c>
#include <stdio.h>
#include "object.h"
#include "misc.h"
#include "noun.h"
#include "move.h"
void executeGet(const char *noun)
{
OBJECT *obj = getVisible("what you want to get", noun);
if (obj == NULL)
{
// already handled by getVisible
}
else if (obj == player)
{
printf("You should not be doing that to yourself.\n");
}
else if (obj->location == player)
{
printf("You already have %s.\n", obj->description);
}
else if (obj->location == guard)
{
printf("You should ask %s nicely.\n", obj->location->description);
}
else
{
moveObject(obj, player);
}
}
void executeDrop(const char *noun)
{
moveObject(getPossession(player, "drop", noun), player->location);
}
void executeAsk(const char *noun)
{
moveObject(getPossession(actorHere(), "ask", noun), player);
}
void executeGive(const char *noun)
{
moveObject(getPossession(player, "give", noun), actorHere());
}
void executeInventory(void)
{
if (listObjectsAtLocation(player) == 0)
{
printf("You are empty-handed.\n");
}
}
<inventory.h>
extern void executeGet(const char *noun);
extern void executeDrop(const char *noun);
extern void executeAsk(const char *noun);
extern void executeGive(const char *noun);
extern void executeInventory(void);
<misc.c>
#include <stdio.h>
#include "object.h"
OBJECT *actorHere(void)
{
OBJECT *obj;
for (obj = objs; obj < endOfObjs; obj++)
{
if (obj->location == player->location && obj == guard)
{
return obj;
}
}
return NULL;
}
int listObjectsAtLocation(OBJECT *location)
{
int count = 0;
OBJECT *obj;
for (obj = objs; obj < endOfObjs; obj++)
{
if (obj != player && obj->location == location)
{
if (count++ == 0)
{
printf("You see:\n");
}
printf("%s\n", obj->description);
}
}
return count;
}
<misc.h>
extern OBJECT *actorHere(void);
extern int listObjectsAtLocation(OBJECT *location);
<move.c>
#include <stdio.h>
#include "object.h"
static void describeMove(OBJECT *obj, OBJECT *to)
{
if (to == player->location)
{
printf("You drop %s.\n", obj->description);
}
else if (to != player)
{
printf(to == guard ? "You give %s to %s.\n" : "You put %s in %s.\n",
obj->description, to->description);
}
else if (obj->location == player->location)
{
printf("You pick up %s.\n", obj->description);
}
else
{
printf("You get %s from %s.\n",
obj->description, obj->location->description);
}
}
void moveObject(OBJECT *obj, OBJECT *to)
{
if (obj == NULL)
{
// already handled by getVisible or getPossession
}
else if (to == NULL)
{
printf("There is nobody here to give that to.\n");
}
else if (obj->location == NULL)
{
printf("That is way too heavy.\n");
}
else
{
describeMove(obj, to);
obj->location = to;
}
}
<move.h>
extern void moveObject(OBJECT *obj, OBJECT *to);
Image of the issue in VSC:
https://i.stack.imgur.com/5mkzV.png

Your code doesn't compile as it relies on header files you haven't shared with us, and executeAsk() for which we have no implementation. I removed the missing bits from parsexec.c:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool parseAndExecute(char *input) {
char *verb = strtok(input, " \n");
char *noun = strtok(NULL, " \n");
if (verb != NULL) {
printf("I don't know how to '%s'.\n", verb);
}
return true;
}
and was then able to build and execute your game:
gcc main.c parsexec.c -o game && ./game
Welcome to Little Cave Adventure.
I don't know how to 'look'.
--> look
I don't know how to 'look'.
--> ^C
This tells me it's a build issue. Specifically, you are probably not linking parsexec.o to your binary.

It turned out to be a faulty compiler/settings in VSC as the code ran fine on a different machine.

Related

Problem with playing the next song automatically with SDL_Mixer

I have this c program which used to search for the mp3 files of given directory and add the files data to linked list.i use SDL2 for audio handling. so much far i would be able to get the playing of mp3 file and what i'm facing here is that i don't know how to play the next song when the first song is played in the linked list.
Here is my code for the program.
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "file_handle.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
struct tags
{
const char *artist;
} tags;
Mix_Music *music = NULL;
bool loadmedia(char *filepath);
void play();
void init();
void load_file_directory(char *dir_path);
int main(int argc, char *argv[])
{
char *file_path;
if (argc <= 1)
{
fprintf(stderr, "no arguments provided!\n");
}
else
{
file_path = argv[2];
}
if (file_path)
{
load_file_directory(file_path);
}
init();
int count = 0;
for (struct song *node = head; node; node = node->next)
{
printf("%s\n", node->file_path);
}
for (struct song *node = head; node; node = node->next)
{
printf("%s\n", node->file_path);
printf("count is %d\n", count);
count++;
Mix_Music *song = Mix_LoadMUS(node->file_path);
if (Mix_PlayMusic(song, 1) != 0)
{
printf("something\n");
}
while (!SDL_QuitRequested())
SDL_Delay(250);
Mix_FreeMusic(song);
}
// char *file = argv[1];
// bool status = loadmedia(file);
// if(status){
// printf("%s - %s\n",Mix_GetMusicArtistTag(music),Mix_GetMusicTitleTag(music));
// if(Mix_PlayMusic(music,1)){
// printf("something wrong");
// Mix_FreeMusic(music);
// }
// }
// while(!SDL_QuitRequested())
// SDL_Delay(250);
}
bool loadmedia(char *file_path)
{
bool success = false;
if (file_path)
{
music = Mix_LoadMUS(file_path);
success = true;
}
else
{
printf("File Loaded Failed\n");
}
return success;
}
void init()
{
bool success;
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
success = false;
}
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3)
{
fprintf(stderr, "could not initialized mixer\n", Mix_GetError());
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
printf("SDL_mixer could not be initialized %s\n", Mix_GetError());
success = false;
}
}

How do I fix undefined reference to `fsm' collect2: error: ld returned 1 exit status?

I have started to learn C recently.
I don't know what's wrong with my code. I keep getting:
undefined reference to 'fsm'
collect2: error: ld returned 1 exit status
Can you please help to fix it?
1) This is my fsm_main.c file.
#include <stdio.h>
#include <string.h>
#include "cstack.h"
#include "fsm.h"
int main(){
char string_input[100];
printf("Enter a string: ");
gets(string_input);
printf("string : %s\n",string_input);
if (fsm(string_input))
printf("All matched correctly\n");
return 0;
}
2) Boolean.h file
#ifndef BOOLEAN_H
#define BOOLEAN_H
typedef int boolean;
#define TRUE 1
#define FALSE 0
#endif
3) cstack.h file
#ifndef CSTACK_H
#define CSTACK_H
#include "boolean.h"
typedef struct cstacknode{
char data;
struct cstacknode *next;
}*cstack;
void init_cstack(cstack *);
void cpush(cstack*,char);
char cpop(cstack *);
boolean is_cfull(void);
boolean is_cempty(cstack);
void print_cstack(cstack);
#endif
4) cstack.c file
#include <stdio.h>
#include <stdlib.h>
#include "boolean.h"
#include "cstack.h"
void init_cstack(cstack *s){
(*s) = NULL;
}
void cpush(cstack*s, char x){
cstack temp;
temp= (cstack)malloc(sizeof(struct cstacknode));
temp -> data = x;
temp -> next = (*s);
(*s) = temp;
}
char cpop(cstack *s){
cstack temp;
char data_popped;
temp = *s;
data_popped =temp->data;
*s =temp->next;
free(temp);
return data_popped;
}
void print_cstack( cstack s){
if(!is_cempty(s)){
printf("%d\n", s->data);
print_cstack(s->next);
}
}
boolean is_cfull(void){
cstack temp;
temp = (cstack) malloc (sizeof(struct cstacknode));
if(temp == NULL)
return TRUE;
else {
free(temp);
return FALSE;
}
}
boolean is_cempty( cstack s){
if (s == NULL)
return TRUE;
else
return FALSE;
}
5) fsm.h file
#ifndef FSM_H
#define FSM_H
#include "boolean.h"
boolean fsm(char *);
boolean is_open(char);
boolean is_close(char);
boolean is_brother(char,char);
#endif
6) fsm.c file
#include <stdio.h>
#include <stdlib.h>
#include "fsm.h"
#include "cstack.h"
boolean is_open(char c) {
return ((c == '{') || (c == '[') || (c == '{'));
}
boolean is_close(char c) {
return ((c == '}') || (c == ']') || (c == '}'));
}
boolean is_brother(char op, char cl) {
return (((op == '{') && (cl == '}')) || ((op == '[') && (cl == ']')) || ((op == '{') && (cl == '}')));
}
boolean fsm(char my_input[]) {
int i = 1;
int nextstate = 0;
cstack top;
char o, c;
init_cstack( & top);
while (1) {
switch (nextstate) {
case 0:
i++;
if (is_open(my_input[i]))
nextstate = 1;
else if (is_close(my_input[i]))
nextstate = 2;
else if (my_input[i] == '0')
nextstate = 3;
else
nextstate = 4;
break;
case 1:
if (!is_cfull()) {
cpush( & top, my_input[i]);
nextstate = 0;
} else {
printf("Error! stack is full\n");
exit(0);
}
break;
case 2:
if (!is_cempty(top)) {
if (is_brother(cpop( & top), my_input[i])) {
nextstate = 0;
} else {
// printf("Error! open and close delimiter do not match\n");
printf("Error! open and close delimiters do not match\n");
return FALSE;
}
} else {
printf("Error! No open delimiter to compare to\n");
return FALSE;
}
break;
case 3:
if (is_cempty(top)) {
return TRUE;
}
else {
printf("Error! stack is full\n");
return FALSE;
}
break;
case 4:
nextstate = 0;
}
}
}

Problems with JSMN API

I'm struggling to figure out how the jsmn library works. Here is my current code and what it produces. My problem is based only in the derefBy function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "jsmnutil.h"
char * jsmnTypes[] = {"undefined","object","array","string","primitive"};
const jsmnmarker_t jsmnErrorMarker = {NULL,-1};
jsmnmarker_t
makeMarker(jsmndata_t * data) {
jsmnmarker_t marker;
marker.data = data;
marker.index = 0;
return marker;
}
int
areEqualMarkers(jsmnmarker_t marker1, jsmnmarker_t marker2) {
return marker1.data == marker2.data && marker1.index == marker2.index;
}
int
isErrorMarker(jsmnmarker_t marker) {
return areEqualMarkers(marker,jsmnErrorMarker);
}
jsmnmarker_t
objectDeref(jsmnmarker_t marker, char * key) {
if (marker.index == -1) return marker;
int childCnt = TOKEN_SIZE(marker);
jsmnmarker_t child = marker;
child.index++;
while (childCnt > 0) {
if (TOKEN_PARENT(child) == marker.index) {
if (strncmp(TOKEN_ADDR(child),key,TOKEN_LENGTH(child)) == 0) {
child.index++;
return child;
}
childCnt--;
}
child.index++;
}
return jsmnErrorMarker;
}
jsmnmarker_t
arrayDeref(jsmnmarker_t marker, int index) {
if (marker.index == -1) return marker;
int childCnt = TOKEN_SIZE(marker);
if (index >= childCnt) return jsmnErrorMarker;
jsmnmarker_t child = marker;
child.index++;
while (1) {
if (TOKEN_PARENT(child) == marker.index) {
if (index == 0) {
return child;
}
index--;
}
child.index++;
}
return jsmnErrorMarker;
}
jsmnmarker_t
derefBy(jsmnmarker_t marker, char * fmt, ...) {
va_list ap;
char * s;
int d;
jsmnmarker_t holder = marker;
va_start(ap,fmt);
while (*fmt) {
switch(*fmt++) {
case 's':
s = va_arg(ap,char *);
printf("string %s\n",s);
break;
case 'd':
d = va_arg(ap,int);
printf("%d\n",d);
break;
}
}
va_end(ap);
}
jsmnmarker_t
nextKeyForObject(jsmnmarker_t objMarker, jsmnmarker_t keyMarker) {
}
jsmnmarker_t
valueForKey(jsmnmarker_t keyMarker) {
keyMarker.index++;
return keyMarker;
}
char * *
keysForObject(jsmnmarker_t marker) {
if (marker.index == -1) return NULL;
if (TOKEN_TYPE(marker) != JSMN_OBJECT) return NULL;
int childSize = TOKEN_SIZE(marker);
char * * keys = malloc((childSize + 1) * sizeof(char *));
jsmnmarker_t child = marker;
child.index++;
int cnt = 0;
while (cnt < childSize) {
if (TOKEN_PARENT(child) == marker.index) {
keys[cnt] = tokenText(child);
cnt++;
}
child.index++;
}
keys[cnt] = NULL;
return keys;
}
char *
tokenText(jsmnmarker_t marker) {
if (marker.index < 0) return NULL;
return strndup(TOKEN_ADDR(marker),TOKEN_LENGTH(marker));
}
void
printToken(FILE * fh, jsmnmarker_t marker) {
if (marker.index >= 0 ) {
fprintf(fh,"%.*s",TOKEN_LENGTH(marker),TOKEN_ADDR(marker));
}
else {
fprintf(fh,"unresolved reference");
}
}
void
dprintToken(int fd, jsmnmarker_t marker) {
if (marker.index >= 0 ) {
dprintf(fd,"%.*s",TOKEN_LENGTH(marker),TOKEN_ADDR(marker));
}
else {
dprintf(fd,"unresolved reference");
}
}
I know I have to return a jsmn marker, but I'm not quite sure how to create it with just the first or last name. Here is an example code that uses this function.
char * firstName = tokenText(derefBy(faculty,"ds",i,"firstName"));
char * lastName = tokenText(derefBy(faculty,"ds",i,"lastName"));
fprintf(stdout,"Prof. %s %s \n",firstName,lastName);
My confusion is based on the results. It seems completely random that I either get
Faculty Schedules
0
string firstName
0
string lastName
Prof. (null) (null)
0
string classes
Segmentation fault (core dumped)
Or I get this without changing anything
Faculty Schedules
0
string firstName
Segmentation fault (core dumped)
I want it to simply produce
Prof. <firstName> <lastName>
Classes: ...
fmt is a string of 's' and 'd' characters which describes whether the remaining arguments are object keys or array indices. For example, derefBy(marker,"sds",key1, sub1, key2) would dereference marker first by key1 then index sub1 and finally key2

Getting memory leak but memory allocated was deallocated

C noob over here. Created a program that simulates a soccer team to help me get a handle on memory allocation. My program works but valgrind is telling me that I have a memory leak in the methods "create_player" and "add_player_to_club"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 8
typedef struct player {
int id;
char *position;
} Player;
typedef struct club {
int size;
Player *team[SIZE];
} Club;
Player *create_player(int id, const char *description);
void create_team(Club *club);
void print_club(const Club *club);
void destroy_player(Player *player);
void add_player_to_club(Club *club, int id, const char *position);
void destroy_club(Club *club);
int main() {
Club club;
create_team(&club);
add_player_to_club(&club, 1, "forward");
add_player_to_club(&club, 2, "goalie");
print_club(&club);
destroy_club(&club);
return 0;
}
Player *create_player(int id, const char *description){
Player *player;
player = malloc(sizeof(Player));
if(description == NULL){
player->position = NULL;
} else {
player->position = malloc(strlen(description) + 1);
strcpy(player->position, description);
player->id = id;
}
return player;
}
void destroy_player(Player *player){
if (player == NULL){
return;
} else {
free(player->position);
free(player);
}
}
void create_team(Club *team){
team->size = 0;
}
void print_club(const Club *club) {
int i = 0;
if (club == NULL) {
return;
} else if (club->size == 0) {
printf("No team members\n");
} else {
for (i = 0; i < club->size; i++) {
printf("Id: %d Position: %s\n", club->team[i]->id,
club->team[i]->position);
}
}
}
void add_player_to_club(Club *club, int id, const char *position){
if (club == NULL || club->size >= SIZE) {
return;
} else {
club->team[club->size] = create_player(id, position);
club->size++;
}
}
void destroy_club(Club *club){
int i = 0;
if (club == NULL) {
return;
} else {
club->size = 0;
for (i = 0; i < club->size; i++) {
destroy_player(club->team[i]);
}
}
}
I think the problem might be with my "destroy club" method. Player "objects" are stored in the "team" array. I allocated memory for each player object and deallocating by iterating through team array and freeing each index. What did I screw up?
In destroy_club, you set size to 0, then use that to loop through the players, so it loops through nothing.
Set size to 0 after cleaning up the players:
for (i = 0; i < club->size; i++) {
destroy_player(club->team[i]);
}
club->size = 0;

void * function pointer

I have made a typedef for a function pointer that takes in an integer and returns a void *:
typedef void* (*fp)(int index);
I then made a struct that contains a fp and another struct of the same type:
typedef struct fp_holder {
fp function_pointer;
iterable *next;
} fp_holder;
I am trying to figure out how to call fp inside a fp_holder.
To test this out, I did the following:
void *test_fp(int index) {
if (index == 0) {
printf('H');
fflush(stdout);
return [something_that_works];
}
else if (index == 1) {
printf('e');
fflush(stdout);
return [something_that_works];
}
else if (index == 2) {
printf('l');
fflush(stdout);
return [something_that_works];
}
else if (index == 3) {
printf('l');
fflush(stdout);
return [something_that_works];
}
else if (index == 4) {
printf('o');
fflush(stdout);
return [something_that_works];
}
else {
return (void *) NULL;
}
}
fp_holder *a = (fp_holder *) malloc(sizeof(fp_holder));
a->function_pointer = test_fp;
a->next = NULL;
So with all that setup, I tried to call a's function_pointer by trying the following:
a->function_pointer(0);
(*a->function_pointer)(0);
((*)a->function_pointer)(0);
I just cannot figure out why those are not working.
Help would be appreciated! :)
EDIT
What I am trying to do:
Call a's function_pointer with an argument.
I will try out some answers right now and see what happens.
EDIT2
Answered! I was calling it right by doing a->function_pointer(0) but what was giving me a segmentation error [which is what my issue was - and maybe I should have clarified this] was the printf statement and NOT my call. printf needs a string not the char as I put in.
Does your original code actually do
printf('H');
instead of
printf("H");
?
Simplified version of the code you posted, with correct arguments to printf:
#include <stdio.h>
typedef void* (*function_pointer_t)(int index);
struct function_holder {
function_pointer_t callback;
};
void* testFn(int i)
{
printf("testFn %d\n", i);
}
int main(void) {
struct function_holder fh = { testFn };
struct function_holder* fhp = &fh;
fh.callback = testFn;
fh.callback(1);
fhp->callback(2);
return 0;
}
works as expected: http://ideone.com/1syLlG
your code has couple of errors. I am not sure what you are trying to achieve by this code, however here is your working code.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef void* (*fp)(int index);
typedef struct fp_holder {
fp function_pointer;
struct fp_holder *next;
} fp_holder;
void *test_fp(int index) {
if (index == 0) {
printf("H");
fflush(stdout);
return "H";
}
else if (index == 1) {
printf("e");
fflush(stdout);
return "e";
}
else if (index == 2) {
printf("l");
fflush(stdout);
return "l";
}
else if (index == 3) {
printf("l");
fflush(stdout);
return "l";
}
else if (index == 4) {
printf("o");
fflush(stdout);
return "o";
}
else {
return (void *) NULL; }
}
int main() {
fp_holder *a = (fp_holder *) malloc(sizeof(fp_holder));
a->function_pointer = test_fp;
a->next = NULL;
a->function_pointer(0);
}

Resources