I know how to pass an unknown amount of arguments from unknown types to a function.
I mean something like this:
char* plugins_entry(const char* data, ...);
Now I am trying to pass a struct besides a few other arguments to that function. I have the same struct declared (or defined?) in both files (main.c and plugins.c). But when I try to “filter” the irc struct passed from the main.c out and parse the data in the irc struct from the plugins.c, I don’t get anything usefull. When I call the function I get a segementation fault.
This is the important part from the main.c:
struct irc_data {
char nick[32];
char user[32];
char host[64];
char chan[32];
char message[512];
int is_ready;
};
....
int main(int argc, char** argv) {
....
struct irc_data *irc = malloc(sizeof(struct irc_data));
....
(*lib_plugin)("r",irc); // call the function in plugins.c
....
}
And here the hole plugins.c:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
struct irc_data{
char nick[32];
char user[32];
char host[64];
char chan[32];
char message[512];
int is_ready;
};
char* plugins_entry(const char* data, ...) {
int i;
struct irc_data *irc = malloc(sizeof(struct irc_data));
va_list args;
va_start(args, data);
for(i=0; data[i] != '\0'; ++i){
if(data[i] == 'r'){
irc = data[i];
}
}
va_end(args);
printf("\n\n------------------------\n");
printf("What we got here: %s\n",irc->nick);
printf("\n------------------------\n\n");
return "done";
}
So, I am pretty sure that I am doing something wrong with the pointers.
Could you please help me out here?
Thank you
~ Tectu
The line
irc = data[i]doesn’t make any sense. You need to callva_arg:Also, the
mallocinplugins_entryis useless, it will only leak memory.