On Friday, November 20, 2015 at 5:34:33 PM UTC, Liz Huang wrote:
> Hi,
>
> I am trying to use Fiddle to call C function in dynamic library, I used to be able to
> pass a return long variable and an error message, but now only return long variable
> is returned, can't get the error message, I create a very simple example to test,
> this is my add.c
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> long add(long maxn, double delta, double conf, char *errMsg)
> {
> long answer;
>
> errMsg = (char *) malloc(6*sizeof(char));
> answer = (long)(maxn + delta + conf);
> errMsg = "Hello!";
Arguments are passed by value in C, so if you assign to a variable in a function it does not change its value in the calling function. You need to change your C function so that either:
- the argument is a pointer to some memory allocated by the caller and you copy into it by strncpy or similar
- the argument is a pointer to a pointer size block of memory. Your function would then allocate a buffer, write the error message to that buffer and then write the value of the pointer to the argument, ie the last argument to the function is now char **message and your code does *message = malloc(...)
Fred