How to call a static function - c

I'm trying to call the following function
In daydream.c :
.....
static int create_new_account(void)
{
DDPut(sd[newucstr]);
switch (HotKey(HOT_NOYES)) {
case 1:
if (CreateNewAccount()) {
clog.cl_userid = user.user_account_id;
clog.cl_firstcall = user.user_firstcall;
clog.cl_logon = time(0);
if (user.user_connections == 0)
clog.cl_flags |= CL_NEWUSER;
clog.cl_bpsrate = bpsrate;
getin();
return 1;
}
return 0;
case 2:
DDPut("\n");
return 0;
default:
return 1;
}
}
From matrix.c:
int apply()
{
create_new_account();
}
However, it won't compile link:
matrix.o: In function `apply':
matrix.c:(.text+0xf0): undefined reference to `create_new_account'
collect2: error: ld returned 1 exit status
make: *** [daydream] Error 1
So, my question is, how do I call this function properly?

You must not know what the keyword static means. static keeps create_new_account global to daydream.c only. Nobody else can access it. If you want other functions to access it:
Remove the keyword static and extern this function into matrix.c.
Option 1:
/* daydream.c */
int create_new_account(void)
{
...
}
/* matrix.c */
extern create_new_account(void);
Option 2:
/* daydream.c */
int create_new_account(void)
{
....
}
/* daydream.h */
extern int create_new_account(void);
/* matrix.c */
#include "daydream.h"
Create a external wrapper function to call that in turn calls your static function.
/* daydream.c */
static int create_new_account(void)
{
...
}
int create_new_account_wrapper(void)
{
return create_new_account();
}
/* matrix.c */
int apply(void)
{
return create_new_account_wrapper();
}
My preference is answer 1, option 2.

Related

Using a loop to call 'sequentially' named functions in C

Say I have functions foo_1(), foo_2(),...foo_n()
How could I use a loop to call them, that is how could 'convert' a string to a function call:
for (i = 0; i < n; i++)
switch (fork()) {
case 0: //child process
*COMVAR+=m;
//call foo_i()
exit(4);
case -1:
exit(5);
}
You cannot have the compiler or runtime do this automatically in C, but you can manually list the function pointers and invoke them in a loop, i.e.:
// create your function prototype, which all functions must use
typedef void(*VoidFunc)(void);
// create the array of pointers to actual functions
VoidFunc functions[] = { foo_1, foo_2, foo_3 };
// iterate the array and invoke them one by one
int main(void)
{
for (int i = 0; i < sizeof(functions) / sizeof(*functions); i++)
{
VoidFunc fn = functions[i];
fn();
}
return 0;
}
Keep in mind that void func() is not the same as void func(void) in C.
Nope.
The best you can do involves an array of function pointers
#include <stdio.h>
typedef int (*fx)(void); // fx is pointer to function taking no parameters and returning int
int foo_1(void) { printf("%s\n", __func__); return 1; }
int foo_2(void) { printf("%s\n", __func__); return 2; }
int foo_three(void) { printf("%s\n", __func__); return 3; }
int main(void) {
fx foo[3] = { foo_1, foo_2, foo_three };
for (int k = 0; k < 3; k++) {
printf("foo[%d]() returns %d\n", k, foo[k]());
}
}
see code running on ideone
Generally , your approach isn't possible in C.
But you can do it by using switch statement.
Though you have to write little bit of code.
switch (n)
​{
case 1:
foo_1();
break;
case 2:
foo_2();
break;
case 3:
foo_3();
break;
.
.
.
case n:
foo_n();
break;
default:
// code to be executed if n doesn't match any constant
}
Higher level languages that have reflection such as Java is able to do such kind of operations, but C does not. In Java, you can do something like below:
You have a class named MyClass
public class MyClass {
public void myMethodName(String arg1);
}
You can use below flow to call myMethodName using String form.
Class myObject = new MyClass();
Class<?> c = Class.forName("MyClass");
Class[] argTypes = new Class[] { String[].class };
Method method = c.getDeclaredMethod("myMethodName", argTypes);
method.invoke(myObject, params);
Here is the official document: https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html

How to wrap function pointer in plain C

Is it possible to "wrap" a function pointer in C somehow, similar to what you would do with a lambda in C#?
Actual problem I am having is:
I have a couple of functions with different parameters:
// more than two in actual code
void DoStuff(void) { ... }
void DoOtherStuff(int) { ... }
...and I want to create a couple of threads to run these in a loop:
// this won't work because it expects a LPTHREAD_START_ROUTINE,
// which is int(*fn)(void*)
tHnd1 = CreateThread(NULL, 0, &DoStuff, NULL, 0, &tId);
tHnd2 = CreateThread(NULL, 0, &DoOtherStuff, NULL, 0, &tId);
In C#/C++ I would use a lambda, or a pointer to a method which would call the other one, but I have no clue how to do this in C, unless I manually create wrapper functions:
int CallDoStuff(void *dummy) { DoStuff(); return 0; }
int CallDoOtherStuff(void *dummy) { DoOtherStuff(42); return 0; }
Is there any other way to avoid doing this step?
Nope, there's really no other way than to create the wrapper functions. And remember they have to return a value as well. If you don't wrap (or forget to return a (dummy) value) you will have UB.
You can create structure which will contain function type, function pointer and arguments if needed. Thread function would have to check function type and then call function using appropriate signature and pass parameters stored in structure. You can also create helper functions used to create these structures to simplify coding. Below is example code for two possible function types (with void and int arg):
#include <stdio.h>
#include <stdlib.h>
/* Few types needed to store function pointer and arguments in struct */
typedef enum FuncType
{
F_Void,
F_Int,
} FuncType;
typedef void(*VoidFuncPtr)(void);
typedef void(*IntFuncPtr)(int);
typedef struct FuncWrapper
{
FuncType funcType;
union
{
VoidFuncPtr voidFunc;
IntFuncPtr intFunc;
};
union
{
int intArg;
};
} FuncWrapper;
/* Thread func which can handle different functions */
void ThreadFunc(void* arg)
{
FuncWrapper* wrapper = (FuncWrapper*)arg;
switch (wrapper->funcType)
{
case F_Void:
wrapper->voidFunc();
break;
case F_Int:
wrapper->intFunc(wrapper->intArg);
break;
}
free(wrapper);
}
/* Helper functions used to create FuncWrapper instances */
FuncWrapper* wrapVoidFunc(VoidFuncPtr func)
{
FuncWrapper* wrapper = (FuncWrapper*)malloc(sizeof(FuncWrapper));
wrapper->funcType = F_Void;
wrapper->voidFunc = func;
return wrapper;
}
FuncWrapper* wrapIntFunc(IntFuncPtr func, int arg)
{
FuncWrapper* wrapper = (FuncWrapper*)malloc(sizeof(FuncWrapper));
wrapper->funcType = F_Int;
wrapper->intFunc = func;
wrapper->intArg = arg;
return wrapper;
}
/* Dummy StartThread func, which simply calls passed in function */
typedef void(*ThreadFuncPtr)(void*);
void StartThread(ThreadFuncPtr funcPtr, void* data)
{
funcPtr(data);
}
/* Functions which will be called */
void myVoidFunction(void)
{
printf("myVoidFunction called\n");
}
void myIntFunction(int arg)
{
printf("myIntFunction called, arg = %d\n", arg);
}
/* Finally the main func */
int main()
{
StartThread(ThreadFunc, wrapVoidFunc(myVoidFunction));
StartThread(ThreadFunc, wrapIntFunc(myIntFunction, 22));
return 0;
}

MINIX 2 - syscall to kernel

I want to make 2 syscalls to kernel (getlot and setlot). They have to read and set some value in struct proc, which is in kernel. What is the problem? What is missing?
In /usr/include/minix/callnr.h I increased NCALLS and add 2 define
#define NCALLS 80 /* number of system calls allowed */
#define SETLOT 78
#define GETLOT 79
In usr/src/mm/main.c
PUBLIC void do_setlot()
{
message msg;
msg = mm_in;
_taskcall(SYSTASK, SYS_SETLOT), &msg);
}
PUBLIC void do_getlot()
{
message msg;
msg = mm_in;
_taskcall(SYSTASK, SYS_GETLOT, &msg);
}
In /usr/src/mm/proto.h I added two prototypes
_PROTOTYPE( void do_setlot, (void));
_PROTOTYPE( void do_getlot, (void));
In /usr/src/mm/table.c I added to the end of _PROTOTYPE (int (*call_vec[NCALLS]), (void) )
do_setlot,
do_getlot,
In /usr/src/fs/table.c I added to the end of_PROTOTYPE (int (*call_vec[]), (void) )
no_sys,
no_sys,
In /usr/include/minix/com.h I created 2 SYS_xxx define
# define SYS_SETLOT 22
# define SYS_GETLOT 23
In /usr/src/kernel/system.c I wrote
FORWARD _PROTOTYPE( int do_procsetlot, (message *m_ptr) );
FORWARD _PROTOTYPE( int do_procgetlot, (message *m_ptr) );
Then added SYS_xxx to switch in PUBLIC void sys_task()
case SYS_SETLOT: r = do_procsetlot(&m); break;
case SYS_GETLOT: r = do_procgetlot(&m); break;
And at the bottom I wrote 2 definions
PRIVATE int do_procsetlot(m_ptr)
register message *m_ptr;
{
pid_t prid;
int i;
int tickets;
prid = m_ptr->m1_i1;
tickets = m_ptr->m1_i2;
if(tickets > LOT_MAX)
return EINVAL;
if(tickets < LOT_MIN)
return EINVAL;
for(i = 0 ; i <NR_PROCS; i++)
{
if(proc[i].p_pid == prid)
{
proc[i].tickets_number = tickets;
return 0;
}
{
return EINVAL;
}
PRIVATE int do_procgetlot(m_ptr)
register message *m_ptr;
{
int i;
pid_t prid;
prid = m_ptr->m1_i1;
for(i = 0 ; i< NR_PROCS; i++)
{
if(proc[i].p_pid == prid)
return proc[i].tickets_number;
}
return EINVAL;
}
Finally after make hdboot I got error:
exec cc -c -I/usr/include main.c
exec cc -c -I/usr/include proc.c
exec cc -c -I/usr/include system.c
"system.c", line 1260: static not expected
make in /usr/src/kernel: Exit code 1
Line 1260 is
PRIVATE int do_procgetlot(m_ptr)
I know this message is years old, but...
You have a syntax error at the bottom of do_procsetlot:
{
return EINVAL;
}
...should be:
}
return EINVAL;
}
I hope you figured it out yourself some time in 2014!
BTW, the Minix 2 compiler totally supports ANSI C, so you shouldn't need all the K&Risms.

Compile error when creating packet

I'm studying THIS tutorial for tinyos and I wanted to try it out. I try to create the packet but it gives me the following error. I don't know what's wrong. It is probably something simple but I can't figure out what it is.
#include "TestMsg.h"
...
event void AMControl.startDone(error_t error) {
if (error == SUCCESS) {
call Leds.led0On();
//create packet
TestMsg_t* msg = call Packet.getPayload(&packet, sizeof(TestMsg_t));
msg->NodeID = TOS_NODE_ID;
//
// //TODO in the meantime this can change
// button_state_t val = call Get.get();
// msg->Data = ( val == BUTTON_PRESSED ? 1 : 0 );
//
// //send packet
// if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
// radioBusy = TRUE;
// }
} else {
call AMControl.start();
}
}
...
Here is TestMsg.h
#ifndef TEST_MSG_H
#define TEST_MSG_H
typedef nx_struct _TestMsg {
nx_uint16_t NodeID;
nx_uint8_t Data;
} TestMsg_t;
enum {
AM_RADIO = 6
};
#endif /* TEST_MSG_H */
Here is the part where it is declared in the video
The error I get it this:
In file included from /home/advanticsys/ws/TestRadio/src/TestRadioAppC.nc:5:
In component `TestRadioC':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc: In function `AMControl.startDone':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:43: syntax error before `*'
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: `msg' undeclared (first use in this function)
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: (Each undeclared identifier is reported only once
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: for each function it appears in.)
Update
Something is wrong with structs and headers.
#include "Szar.h"
#include "BarType.h"
module SzarP {
uses interface Boot;
uses interface Leds;
}
implementation {
event void Boot.booted() {
// TODO Auto-generated method stub
call Leds.led0On();
Szar_t foo;
Szar_t *szar = &foo;
BarType_t barVar;
barVar.data = 0;
BarType_t *pBarVar = &barVar;
pBarVar->data = 1;
}
}
Here are the 2 header files.
#ifndef SZAR_H
#define SZAR_H
typedef nx_struct _Szar {
nx_uint8_t szar1;
nx_uint16_t szar2;
} Szar_t;
#endif /* SZAR_H */
#ifndef BAR_TYPE_H
#define BAR_TYPE_H
typedef struct _BarType {
uint8_t id;
uint32_t data;
} BarType_t;
#endif /* BAR_TYPE_H */
And the errors:
In file included from /home/advanticsys/ws/Szar/src/SzarAppC.nc:6:
In component `SzarP':
/home/advanticsys/ws/Szar/src/SzarP.nc: In function `Boot.booted':
/home/advanticsys/ws/Szar/src/SzarP.nc:15: syntax error before `foo'
/home/advanticsys/ws/Szar/src/SzarP.nc:19: `barVar' undeclared (first use in this function)
/home/advanticsys/ws/Szar/src/SzarP.nc:19: (Each undeclared identifier is reported only once
/home/advanticsys/ws/Szar/src/SzarP.nc:19: for each function it appears in.)
/home/advanticsys/ws/Szar/src/SzarP.nc:20: syntax error before `*'
/home/advanticsys/ws/Szar/src/SzarP.nc:21: `pBarVar' undeclared (first use in this function)
For some strange reason I have to declare EVERY variable outside the function, and then it works. Example:
bool radioBusy = FALSE;
message_t packet;
TestMsg_t *messageToSend;
button_state_t buttonState;
event void AMControl.startDone(error_t error) {
if (error == SUCCESS) {
call Leds.led0On();
messageToSend = call Packet.getPayload(&packet, sizeof(TestMsg_t));
messageToSend->NodeID = TOS_NODE_ID;
//TODO in the meantime this can change
buttonState = call Get.get();
messageToSend->Data = ( buttonState == BUTTON_PRESSED ? 1 : 0 );
//send packet
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
radioBusy = TRUE;
}
} else {
call AMControl.start();
}
}
It also works if I declare my variables at the beginning of the functions/events/commands without any code before them.

"called object is not a function" error - C

int getSpeedOfMotorInPercent(int RPM)
{
int speedOfMotor = (RPM/5000.0)*100;
return speedOfMotor;
}
static char *test_GetSpeedOfMotor(int speedInPercent)
{
mu_assert("error, RPM != 70%", speedInPercent == 70);
return 0;
}
static char *run_all_tests(int RPM)
{
mu_run_test(test_GetSpeedOfMotor(RPM));
return 0;
}
I get the error "called object is not a function" on mu_run_test(test_GetSpeedOfMotor(RPM));
I tried removing the pointer of the function but then I get even more errors.
EDIT:
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(test) do { char *message = test(); tests_run++; if (message) return message; } while (0)
extern int tests_run;
this is the mu_run_test function. It is provided to me like that in the header file.
You're passing test_GetSpeedOfMotor(RPM) as test in the macro, which will result in this code:
char *message = test_GetSpeedOfMotor(RPM)();
Since you're probably using a test framework which you don't want to change, just remove the RPM parameter from the declaration of test_GetSpeedOfMotor function and use it like this:
int testRpmInPercent;
static char *test_GetSpeedOfMotor()
{
mu_assert("error, RPM != 70%", testRpmInPercent == 70);
return 0;
}
static char *run_all_tests(int RPM)
{
testRpmInPercent = RPM;
mu_run_test(test_GetSpeedOfMotor);
return 0;
}
Then you'll have to find an other way of sharing the RPM value with the test function. Like a global variable or with whatever method the test framework has to offer.
If you're willing to change the test framework, I would modify that define to this (remove () after test):
#define mu_run_test(test) do { char *message = test; tests_run++; if (message) return message; } while (0)

Resources