简体   繁体   English

C ++传递结构地址

[英]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 . 是的,您的代码是可编译的,除了在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 ? 我不太清楚您要传递函数和实际函数调用的确切要求,但是我想您想知道为什么函数调用使用&fnc ,但参数是stuff *pm In that case, the fnc variable declared is a plain stuff . 在这种情况下,声明的fnc变量是简单的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 . 现在multiply函数声明为取stuff* -指针的stuff This means that you can't pass fnc directly -- it's a stuff and multiply expects a *stuff . 这意味着您不能直接传递fnc这是一个stuffmultiply *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 . 但是,您可以使用&运算符将fnc作为stuff*传递fnc地址,并且&fnc是有效的stuff* ,可以将其传递给multiply

Once you're in the multiply function, you now have a stuff* called pm . 进入multiply功能后,您将拥有一个名为pmstuff* 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 . 要从此stuff*获取onetwo变量,请使用指向成员运算符( -> )的指针,因为它们是stuff指针,而不是简单的stuff After obtaining those values ( pm->one and pm->two ), the code then multiples them together before printing them out ( pm->one * pm->two ). 在获得了这些值( pm->onepm->two )之后,代码会将它们pm->two ,然后将它们打印出来( 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 * . 您的pm变量被声明为指针,因此stuff类型用*修改。 Your fnc variable is being used (namely for its address), and thus the variable itself is marked with & . 您的fnc变量正在使用(即用于其地址),因此变量本身用&标记。

You can imagine the above examples as the following (C++ doesn't actually have these, so don't go looking for them): 您可以将上面的示例想象如下(C ++实际上没有这些,因此不要去寻找它们):

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. stuff * pm表示pm是东西结构的地址

The

&fnc

says "the address of fnc ". 说“ fnc的地址”。

When a variable is declared like: 当像这样声明变量时:

stuff *pm; * pm;

it tells us that pm should be treated like an address whose underlying type is stuff . 它告诉我们pm应该被当作一个底层类型是stuff的地址处理。

And if we want to get the address of a variable stuff fnc , we must use 而且,如果我们要获取变量stuff fnc的地址,则必须使用

&fnc &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 . 您可以在此处清楚地看到结果变量先前为零,然后将结构的地址传递给函数后,结果变量值将更新为值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. 当然,除了错误的main函数定义之外,这还行得通。

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 * . 之所以起作用,是因为当您使用一元&运算符时,它实际上返回一个指向操作数的指针,因此,在您的情况下, fncstuff类型的,如果您执行了&fnc ,则将返回stuff * This is why the multiply function must take in a stuff * . 这就是为什么multiply函数必须包含一个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. 在主要方面,您成功创建了一个“ stuff”对象并设置其值。 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. “ const stuff * pm”中的*表示它需要一个指向stuff对象的常量指针。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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