cfunctionpointersparameter-passingaddressof

passing address of variable to function


I am not great on pointers but I have to learn in the field. If my understanding serves me correct these should all be valid statements below.

int* a;
int b = 10;
a = &b;
(*a) = 20;

(*a) == b;  //this should be true

if you have a function like this:

void copy(int* out, int in) {
    *out = in;
}

int m_out, m_in;

copy(&m_out, m_in);

m_out == m_in; // this should also be true

but I saw a function like this

create(float& tp, void* form, char* title);

I understand the void pointer, it can be cast to anything, I understand the character pointer which is basically a c style string.

I do not understand the first argument, which is the address of some type, let's say a float but it could be anything, a struct, a int, etc.

What is going on there?


Solution

  • First this

    int m_out, m_in;
    
    copy(&m_out, m_in);
    

    is undefined behaviour - you passed uninitialized vaiable m_in to function - and hence trying to make copy of an uninitialized variable.

    This:

    create(float& tp, void* form, char* title);
    

    doesn't make sense in C. Looks like reference from C++.