I am trying to make the s_cord_print function visible in the cord_s.c file only. Currently the function is visible/runnable in main.c even when it is declared static.
How do I make the s_cord_print function private to cord_s.c?
Thanks!
s_cord.c
typedef struct s_cord{
int x;
int y;
struct s_cord (*print)();
} s_cord;
void* VOID_THIS;
#define $(EL) VOID_THIS=⪙EL
static s_cord s_cord_print(){
struct s_cord *THIS;
THIS = VOID_THIS;
printf("(%d,%d)\n",THIS->x,THIS->y);
return *THIS;
}
const s_cord s_cord_default = {1,2,s_cord_print};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "s_cord.c"
int main(){
s_cord mycord = s_cord_default;
mycord.x = 2;
mycord.y = 3;
$(mycord).print().print();
//static didn't seem to hide the function
s_cord_print();
return 0;
}
~
The problem is:
You should remove that. Instead, create a
s_cord.hfile that contains only declarations, such as:and put:
in
main.cands_cord.c. You also need anexterndeclaration fors_cord_default. So the complete code is:s_cord.c:
s_cord.h:
main.c:
You’ll now get a error if you try to call
s_cord_print()from main, as expected.EDIT: I forgot to move the
$(EL)definition, and it needed an extern forVOID_THIS.EDIT 2: The correct compilation command is: