0

So I was having some fun with c when I tried this:

#include <stdio.h>
#include <string.h>

typedef void (*Function)(char *);

void helloWorld(char *);
void execute(Function, char *);

Function func;

int main(void){
    char *message = "StackOverflow";
    execute(helloWorld, message);

    printf("%s", message);
    return 0;
}

void helloWorld(char *message){
    printf("HelloWorld, %s.\n", message);
    message = "DONE";
    printf("[%s]\n", message);
}

void execute(Function function, char * msg){
    func = function;
    func(msg);
}

Apparently I am not able to use pointers - which I used as parameter - as return value of pointer functions.

Well, can someone explain this behavior? How can I get return value(s) of void function?

5
  • 1
    Better use const char* with string literals. Also, arguments are passed by value in C. Therefore char** works (I read your answer.) Commented Sep 12, 2015 at 15:31
  • Use proper prototypes. For an empty argument list, (void) is the correct way. Commented Sep 12, 2015 at 15:52
  • Do I actually override the variable with a new pointer? Commented Sep 12, 2015 at 16:10
  • You just asked how to get return value of void function. And even if that made sense, your example does not have any return values except for main. What are trying to do? Commented Sep 12, 2015 at 16:16
  • @MarkLakata you are right. Maybe return value is not the right term. I was just playing around with c. Commented Sep 12, 2015 at 18:00

2 Answers 2

1

So I found a solution while writing the question.

Apparently char pointers are not actually pointers, somehow. When I realised this it tried using pointer to pointer (**) instead and it worked.

#include <stdio.h>
#include <string.h>

typedef void (*Function)(char **);

void helloWorld(char **);
void execute(Function, char **);

Function func;

int main(void){
    char *message = "StackOverflow";
    execute(helloWorld, &message);

    printf("%s\n", message);

    return 0;
}

void helloWorld(char **message){
    printf("HelloWorld, %s.\n", *message);
    *message = "DONE";
    printf("[%s]\n", *message);
}

void execute(Function function, char ** msg){
    func = function;
    func(msg);
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your original code:

void helloWorld(char *message){
    printf("HelloWorld, %s.\n", message);
    message = "DONE";
    printf("[%s]\n", message);
}

the line message = "DONE"; will change the local (or "automatic") variable named message, because function parameters are, for all intents and purposes, local variables in C.

Thus, the value of the message local variable from main will not change, as those are two different variables.

Now, in your second example, you are passing pointers to pointers:

void helloWorld(char **message){
    printf("HelloWorld, %s.\n", *message);
    *message = "DONE";
    printf("[%s]\n", *message);
}

So, your *message = "DONE"; is now changing what the message (parameter) points to, and it is pointing to the message from main(), thus it is changing message from main(). The message from helloWorld() itself is not changed here.

Of course, there is nothing special about character pointers w.r.t other pointers, they are pointers as much as any other. The only special thing is treating string literals as character pointers, but that doesn't matter here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.