简体   繁体   中英

C++ Passing Struct Address

Im a little bit confused about passing structs into functions. I understand pointers and everything.

But for instance:

struct stuff
{
   int one
   int two 
};

int main{
    stuff fnc;
    fnc.two = 2;
    fnc.one = 1;
    multiply(&fnc);

}

void multiply(const stuff * pm){
    cout << pm->one * pm->two;
}

First of all....am i even doing this right. And second of all, why do we use the address operator when we pass the function, but use the * pointer operator in the actual function call? Im confused?

Yes, your code is compilable other than the missing semicolons in the defintion of struct stuff . I'm not quite sure exactly what you're asking about passing the function and the actual function call, but I think you're wondering why the function call uses &fnc , but the parameter is stuff *pm ? In that case, the fnc variable declared is a plain stuff . It is not a pointer, it refers to the actual instance of that struct.

Now the multiply function is declared as taking a stuff* -- a pointer to a stuff . This means that you can't pass fnc directly -- it's a stuff and multiply expects a *stuff . However, you can pass fnc as a stuff* by using the & operator to take the address, and &fnc is a valid stuff* that can be passed to multiply .

Once you're in the multiply function, you now have a stuff* called pm . To get the one and two variables from this stuff* , you use the pointer to member operator ( -> ) since they are pointers to a stuff and not a plain stuff . After obtaining those values ( pm->one and pm->two ), the code then multiples them together before printing them out ( pm->one * pm->two ).

The * and & operands mean different things depending on whether they describe the type or describe the variable:

int  x;        // x is an integer
int* y = &x;   // y is a pointer that stores the address of x
int& z =  x;   // z is a reference to x
int  a = *y;   // a in an integer whose value is the deference of y

Your pm variable is declared as a pointer, so the stuff type is modified with * . Your fnc variable is being used (namely for its address), and thus the variable itself is marked with & .

You can imagine the above examples as the following (C++ doesn't actually have these, so don't go looking for them):

int x;
pointer<int> y = addressof(x);
reference<int> z = x;
int a = dereference(y);

It the difference between describing a type and performing an operation.

In

void multiply(const stuff * pm){
    cout << pm->one * pm->two;
}

The stuff * pm says that pm is an address of a stuff struct.

The

&fnc

says "the address of fnc ".

When a variable is declared like:

stuff *pm;

it tells us that pm should be treated like an address whose underlying type is stuff .

And if we want to get the address of a variable stuff fnc , we must use

&fnc

Following code will tell you about the pointer illustration

A struct address is passed into the function named multiply and this function perform some operations with the element of the passed structure and store the result in the result variable.

you can see here clearly that the result variable is previously zero then after passing the address of the structure to the function multiply the result variable value gets updated to value 6 . this is how pointer works.

#include <iostream.h>



struct stuff
{
   int one;
   int two ;
   int result;
};
void multiply(stuff *pm);
int main(){
    stuff fnc;
    fnc.two = 2;
    fnc.one = 3;
    fnc.result = 0;
    multiply(&fnc);
    cout<<fnc.result;

return 0;

}

void multiply(stuff *pm)
{
    pm->result = pm->one * pm->two;
}

Sure, this would work, aside from your erroneous main function definition.

The reason why this works is because when you use the unary & operator, it essentially returns a pointer to the operand, so in your case, fnc , which is of type stuff , if you did &fnc , that would return a stuff * . This is why the multiply function must take in a stuff * .

struct stuff
{
   int one, two;
};

int main(int argc, const char* argv[]) {
    stuff fnc;
    fnc.two = 2;
    fnc.one = 1;
    multiply(&fnc); //passes a pointer to fnc
}

void multiply(const stuff * pm){
    //the "*" operator is the multiplication operator, not a pointer dereference
    cout << pm->one * pm->two; //->one is equivalent to (*pm).one
}

You need the address of operator so you can get the address of the object, creating a pointer, which the function expects. The '*' in the parameter list is not a pointer operator, it simply says that the variable is a pointer.

Your code is correct. In the main, you successfully create a 'stuff' object and set its values. Then, you pass a constant address to the object into the function multiply. The multiply function then uses that address to access the two variables of the structure to output the multiplication of the variables.

The * in "const stuff * pm" means that it takes a constant pointer to a stuff object.

Here is a working example of what you would like to see.

#include <iostream>
using namespace std;
struct stuff
{
   int one;
   int two;
};

void multiply(stuff* pm)
{
    cout << pm->one * pm->two;
}
int main()
{
    stuff* fnc = new stuff;
    fnc->two = 1;
    fnc->one = 2;
    multiply(fnc);
    delete fnc;
    cin.ignore(1000, 10);
    return 0;
}

You have a couple of syntactic errors in your program, but other than that, the basic idea is fine. Here are the syntax problems I had to fix before your program would compile:

#include <iostream>

using namespace std;

struct stuff
{
   int one;
   int two; 
};

void multiply(const stuff * pm) {
    cout << pm->one * pm->two;
}

int main() {
    stuff fnc;
    fnc.two = 2;
    fnc.one = 1;
    multiply(&fnc);

}

To answer your questions about difference between the '&' (address of) operator and the '*' (pointer dereference) operator though, we just need to think about the types you're passing in to the function.

Take the function multiply:

void multiply(stuff *fnc) {
...
}

In the definition of this function, you are describing something that takes a pointer to a stuff struct. In that first line, you aren't saying you are dereferencing that object, just that you are expecting a pointer to a stuff object.

Now when you call multiply:

stuff fnc;
multiply(&fnc);

You are using the '&' (address of) operator to get a pointer to the object. Since the multiply function expects a pointer, and you have the plain old object, you need to use the & operator to get a pointer to give to the multiply function.

Perhaps it is clearer to think call it like this:

stuff fnc; //The actual object
stuff* fnc_ptr = &fnc; //A pointer to a stuff object, initialized to point at fnc created above
multiply(fnc_ptr); //Call the function with the pointer directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM