passing address of variable to function

3.4k Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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++.

0
On

The first argument is a reference, it just means that if you modify this field in your function create, the field will still remain modified (even in the function where you called create()) because it points to an address and not a value.