Home > database >  Global Array of Functions pointers
Global Array of Functions pointers

Time:09-26

How to make an global array of function in c ?

I want this

//f.cpp
#include "head.h"
void f() {};
//g.cpp
#include "head.h"
void g() {};
//head.h
#pragma once
void f();
void g();

typedef void (*f_t)();
f_t Fs[2] = {f,g};
//main.cpp
#include "head.h"

int main() {
    Fs[0]();
}

Something like this worked for me in C, I'm new to c . I tried a lot of way's of compilling stuff, but each time g gived me "multiple definitions".

How to creat global array without init it throught function ?

I tried to implement same but array in separate "const_array.h" file, it solves probleme, but when I include it to other files, it gives me anyway the same probleme...

CodePudding user response:

Add one more file:

//head.h
#pragma once
void f();
void g();

typedef void (*f_t)();
extern f_t Fs[2];
//head.cpp
#include "head.h"
f_t Fs[2] = {f,g};
  •  Tags:  
  • c
  • Related