void *

Dec 17, 2015 at 10:27am
SOO FRUSTATING, im still a beginner, can anyone explain me how these codes work?

#include <iostream>
using namespace std;

void cg(int * a, int * b, int * c);

int main(){
int x=3, y=4, z=5-x;
cg (&x,&y,&z);
cout<< x << " " << y << " " << z;

return 0;
}

void cg (int * c, int * b, int * a){
(*a)*=3;
(*b)*=2;
(*c)*=1;
}
Last edited on Dec 17, 2015 at 10:28am
Dec 17, 2015 at 10:53am
It's called derefrencing. Take a look at this video - https://www.youtube.com/watch?v=vjq-13YADeI
Dec 17, 2015 at 2:26pm
The arguments to cg() are pointers to integers. Now let's look at the first line of cg():
(*a)*=3;.

(*a) dereferences a, so this expression is the integer that a pointer to. *= multiplies the left side by the right side, so (*a)*=3 is the same as (*a) = (*a) * 3;.

Finally, going back up to main() where cg is called we have cg(&x, &y, &z); Here & takes the address of its argument. So x is an integer, &x is the address of x.
Dec 17, 2015 at 9:52pm
Whoever gave you this code should be tossed into a woodchipper. Look at how the parameter names are changed between the declaration and the definition. The ones in the definition matter, and they are declared arbitrarily in reverse of both their alphabetic order and the order in which they're used in the function. Looks like this code was made to intentionally trip you up. Here's some functionally equivalent code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

//forward declaration, no parameter names needed, only types matter
void cg(int*, int*, int*);

int main()
{
    int x = 3;
    int y = 4;
    int z = (5 - x); //2

    cg (&x,&y,&z);
    cout<< x << " " << y << " " << z;

    return 0;
}

void cg (int * a, int * b, int * c)
{
    (*a) *= 1;
    (*b) *= 2;
    (*c) *= 3;
}
Topic archived. No new replies allowed.