0

I have a structure in my cpp program. Function pointer is one of its members. When this structure is passed to a function-in-dll, it assigns a function to this pointer. So my main program does not know if the dll has assigned a function or not. My aim is to call the function through pointer only if it is assigned through dll. I have made a minimal-working code of the situation:

#include <stdio.h>
typedef struct _test{
        void (*foo)(int a){};
}test;
void bar(int b)
{
        printf("you called bar\n");
}
int main()
{
        test _a;
        test* a=&_a;
        if(a->foo)printf("OK");
        else printf("ELSE");

        a->foo=bar;

        if(a->foo)printf("OK2");
        else printf("ELSE2");
        return 1;
}

The output i get when running is

ELSEOK2

The output is consistent with my expectations. I want know whether i can use

if(a->foo)

to check the situation, so that in future it will not lead to errors. Also is it right to do curly braces { } in end of function pointer

void (*foo)(int a){};

without which i cannot check if foo->a is not zero.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
habi
  • 125
  • 8
  • 2
    Yes, the code looks ok. `{}` here is equivalent to `= nullptr`, normally the latter spelling is used. – HolyBlackCat Jan 01 '23 at 05:59
  • Are you really learning C++? Because with the exception of the initialization of `foo` the code you show is all plain C. What resources are you using to learn C++? – Some programmer dude Jan 01 '23 at 05:59
  • 1
    Also note that whomever told you to `return 1` from the `main` function should be looked at with suspicion, because returning `0` is considered success, while a small non-negative integer (like `1`) is failure. – Some programmer dude Jan 01 '23 at 06:02
  • 1
    `std::function` is the way I would have gone instead of a function pointer. – cplusplusrat Jan 01 '23 at 06:06
  • Regarding the check, if you didn't initialize the variable `foo`, it would have an *indeterminate* value. And in C++ using an indeterminate value of any kind, in any way, will lead to *undefined behavior*. So without the initialization, you simply could not check the pointer if it was valid or not. Using [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) would solve that problem, and possibly more. – Some programmer dude Jan 01 '23 at 06:06
  • 3
    @Andrej Podzimek Your change of the tags is wrong, because the code is invalid in C. C doesn't have inline structure member initialization. I rolled back the edit. – Some programmer dude Jan 01 '23 at 06:07
  • @cplusplusrat Why? – Paul Sanders Jan 01 '23 at 11:14
  • @Someprogrammerdude, thank you i didn't knew returning 0 was traditional way of considering success. from now onwards i will return 0. Thanks for all your answers. I learn C++ by google. We learned ANSI C at school (ten years ago). I am coding with that knowledge, google and projects in our game forum like this one https://bitbucket.org/stormeus/0.4-squirrel/src/master/VCMP.h. Thank you – habi Jan 01 '23 at 18:52
  • 1
    C++ is ***very*** different from C. You can't really use C as a base to learn C++ properly. And you definitely can't Google solutions, that leads down a dangerous [cargo cult](https://en.wikipedia.org/wiki/Cargo_cult_programming) path. If possible invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ properly. – Some programmer dude Jan 01 '23 at 18:55

1 Answers1

1

void (*foo)(int a){}; does initialization to nullptr.

It is equivalent to

void (*foo)(int a) = nullptr;

and

if (a->foo) (or if (a->foo != nullptr)) is correct way to check is pointer is not nullptr.

Jarod42
  • 203,559
  • 14
  • 181
  • 302