answersLogoWhite

0


Best Answer

You can pass the address by using '&' with the pointer variable, while passing actual arguments. In formal arguments '*' is used in the place of '&'. To pass the address of a pointer variable a double pointer variable should be used .

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

structure

{

int a;

int b;

}bar;

void foo(&bar.b);

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Passing the value:

func (strvar.field) or

func (strptr->field)

Passing the address:

func (&strvar.field) or

func (&strptr->field)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which is the syntax used for passing a structure member as an argument to a function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you pass address of the structure member as a function argument?

Yes.


Which is the syntax for passing a structure member as an argument to a function?

struct example { int fld1, fld2; } x1; printf ("fld1=%d, fld2=%d\n", x1.fld1, x1.fld2);


How do you pass a structure member as a argument to a function?

struct example {int fld1, fld2;};struct example x1;printf ("fld1=%d, fld2=%d\n", x1.fld1, x1.fld2);


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


What is a structure member?

methods and variables inside the structure is callled structure member


How would you alter the value stored in the structure member using function?

Pass the structure by reference then dereference the member. typedef struct S { int x; }; void foo (struct S* s) { s->x=42; }


How the member function can be undefined?

You cannot undefine a member function.


What is meant by a structure within a structure?

A structure that is a member of another structure is a structure within a structure.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


What are the applications of the pointer?

The this pointer is used to refer to the object that is foremost in the current scope. It has three primary purposes. First, a programmer uses this to refer to a member variable that has been shadowed by a function argument. Secondly, it is used to obtain a reference to itself, usually for the purpose of passing itself to another function or class. Finally, the this pointer can be used to call alternate constructors of the same object, usually as a means of reusing code and providing various constructors with default arguments.


In the ordered pair x y the value of y is a member of the?

x is a member of the function's domain, y is a member of the function's range.


What do you mean by call value and call by reference?

Call by value and call by reference relate to the way in which arguments are passed into functions. The function itself determines which method is employed. When a function expects an argument by value, the function makes a copy of the argument you pass. That is, you are passing the argument's value, not the argument itself. The value you pass is subsequently assigned to the function argument. If the function argument is an object (an instance of a class), then that class' copy constructor is automatically invoked and the copy is assigned to the function argument. The function argument remains in scope until the function returns, which then invokes the class' destructor. Since the function works with a copy of the argument that you passed, the function has no direct effect on the argument you passed. Passing large and complex objects by value is clearly costly in terms of both performance and memory, but if you want the function to actually make changes to your object, then pass by value will not work unless you also return a copy of the altered object and assign this copy to your original object. That's two copy constructors, two destructors, and one assignment. This is where pass by reference comes in. When you pass by reference you are passing the address of your argument to the function, and that address is assigned to the function argument. So unless the function argument is declared const, any changes made to the function argument will be reflected in the argument you passed. This is clearly more efficient than passing by value, since no copies need to be made. And when the function argument is declared const, you can be assured that your object's immutable members will not be altered by the function since the function can only call const member functions. Passing by reference is clearly the preferred method of passing arguments into functions, particularly when those arguments are complex objects. In order to achieve pass by reference, the function argument needs to accept an address. Assigning the address to a pointer argument seems the obvious way of doing this, but in C++ there is a better way: use a reference! C programmers often get confused when we speak of C++ references because a reference in C is simply another name for a pointer. But in C++, a reference is not a pointer at all. Keep in mind that a pointer is a variable (or a constant), and therefore a pointer has an address of its own because it needs somewhere to store the address that it is pointing at. The stored address is the pointer's value, and that value can be dereferenced provided it is not NULL. That is, dereferencing a pointer gives us the indirect value stored in the address that the pointer is referring to. Moreover, pointer variables can be assigned any address at any time; they can point anywhere, or they can point to NULL. In C++, however, references behave differently to pointers. Once assigned, you cannot reassign a reference and they can never be NULL. Therefore you must assign a reference to an exiting object at the point of instantiation; you cannot declare and assign separately as you can with a pointer. This is because references do not store the address they refer to, they are simply aliases for the address itself. In other words, the address of a reference is the same as the reference itself. In many ways, a reference is not unlike a constant pointer, since it always refers to the same object, but even a constant pointer has its own address, a reference does not. Now that we understand what a reference is, we can see how it can be applied to functions that expect an address. When we pass our argument to a pass by reference function, we are assigning the address of the argument to the function argument, just as we would if the function argument were a pointer. The difference is that we don't need to use the address-of operator to obtain the address since the function already knows to expect a reference. That address is then assigned to the function argument. While the argument remains in scope, we have two guarantees: the reference cannot be NULL (so we don't need to test for NULL as we would with a pointer) and the reference cannot be reassigned (so its effectively the same as a const pointer). Moreover, since it is not a pointer of any kind, there is no redirection involved. The reference can be treated just as if it were the argument we passed; they are one and the same after all, just different names. To conclude, here are some examples of functions that employ pass by value and pass by reference: Example 1: pass by value MyObject foo(MyClass val) { val.set_data(42); return(val); } In this example we are passing a complex object of type MyClass that has a set_data() method. The object we pass is left unaffected because val is a copy of the object we passed. However, if we placed trace code in the class copy constructor and the destructor, we'd find two copies are constructed and destroyed during this call, once to create val, and again to create the return value. val is destroyed first and the return value second. The return value is an automatic variable which we can immediately assign to another object (including the original argument we passed to the function if we wished) but beyond that the return value will cease to exist. To call this function and assign the return value to the original object, we'd use the following: MyClass x; x = foo(x); Example 2: pass by reference (via pointer) void foo(MyClass* const ptr) { if( ptr ) { ptr->set_data(42); // (*ptr).set_data(42); } } Note that the points-to operator (->) is simply sugar-coating for the dereference operator, as shown in the commented line. Note also that the const keyword applies to the pointer itself, not what it points to. Thus *ptr is variable, but ptr is not. Here we're passing a reference by const pointer. Since pointers may be NULL we must ensure the pointer is non-NULL before we attempt to dereference it. Note that we do not need to return anything since ptr is assigned the address of the argument we passed, thus any changes made to the pointer's indirect value are reflected in the argument. In other words, ptr is an output argument. In order to call this function, we must employ the address-of operator. E.g., MyClass x; foo(&x); Example 4: pass by reference void foo(MyObject& ref) { ref.set_data(42); } Here we can see the C++ way of passing by reference. Unlike pointers we don't need to test for NULL, nor do we need to dereference. ref is guaranteed to always refer to a valid object. Moreover, the function call itself is simplified because we don't need to specify the address-of operator. MyClass x; foo(x); Note that ref is simply an alias for x. If we examine the address-of ref (&ref) we will find it is exactly the same as the address-of x (&x). If we look at the address-of ptr (&ptr) in the previous example, we'd find that it was not the same as &x, but its value would be &x, proving that references are neither pointers nor variables of any kind. Note that although these examples use complex objects, the same principal applies to primitives. The only difference is that there is no penalty in passing primitives by value as it's just as quick to pass by value as it is to pass by reference. It's just a question of whether you want changes reflected in the original argument or not. As a final note, if a function expects pass by reference, but you do not wish changes to be reflected in your object, copy the object first and pass the copy instead. This is really no different to pass by value. However, if the function argument is a constant reference, you don't need to make a copy since the function cannot alter the object's immutable members. You can also overload functions to pass by value and by reference, giving the best of both options. However, to achieve this, you need to use the pointer version of pass by reference. This is because the compiler cannot differentiate pass by value from pass by reference any other way.