C++ Programming

Call by reference using pointer in c plus plus?


Top Answer
User Avatar
Wiki User
2013-02-11 05:20:18
2013-02-11 05:20:18


void foo( MyClass& object ){} // function with call by reference signature

MyClass* p = new MyClass(); // instantiate a pointer to MyClass

foo( *p ); // call by reference using the pointer

User Avatar

Related Questions

Call by reference means calling a function using a reference to a variable or a pointer. You call a function by passing refrences to a variable. For eg: void x(int &a) { a=2; } void main() { int s=3; x(s); } OR void a(int &c) { c=5;}void main(){ int *p; *p=2a(*p);}

A reference variable in C++ is a formal parameter of a function call that automatically dereferences itself, as if it were a pointer, into a reference to the original value in the calling routine. You declare the reference type in the function declaration and prototype, but the compiler automatically adds the reference (&) operator on call, and the dereference (*) operator on use.

To access members of an object, use the member-of operator. If using a pointer to an object, use the pointer-to-member operator instead. The following minimal example demonstrates both operators: struct foo { int data; }; int main() { foo f; // static reference foo* p = &f; // pointer to reference f.data=42; // assign by using member-of operator p->data=0; // assign by using pointer-to-member operator }

We use a pointer to reference a string because a string is an array of characters where every element is a char (or a wchar_t if using UNICODE strings). Passing arrays by value would require the entire array to be copied, but passing a pointer variable to an array only copies the pointer, which is effectively the same as passing the array by reference. #include <iostream> int main() { char * psz = "hello"; // pointer to a null-terminated string. std::cout << psz; // pass the pointer (by value) to the insertion operator. return( 0 ); }

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.

No. Calling new returns a pointer to allocated memory. If you re-use a pointer to store the return value, then you must release the memory that it previously pointed at, either by deleting the pointer, or by maintaining a separate pointer to the original memory. Calling new will not release the current memory for you.

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer

C++ uses call by value. Even you you use call by reference semantics, it is still call by value - the value you are passing in that case is simply the address of the argument.

A dangling pointer (we also use the terms stray pointer and wild pointer) is created whenever we call delete on a pointer and then try to use the pointer without reassigning it.We can also create dangling pointers inadvertently by calling a rogue function that returns a pointer to an object that is local to the function we are calling. The object will fall from scope when the function returns so the pointer is left dangling.Note that there is no such thing as a dangling pointer reference. Pointers and references are not the same. A reference is merely an alias to an object -- it consumes no memory beyond the object it refers to. Whereas a pointer is a variable that may contain the address of an object, but it requires additional memory to do so (4 bytes on 32-bit architecture). Pointers may be NULL, references can never be NULL. Pointers to valid objects require indirection, references do not. References are the preferred method of accessing an object's members, not least because they are easier to work with.

generic pointer is a pointer that can not be dereferenced i.e one type pointer can not be assigned to another type.

The ampersand '&' has many uses in the C++ language:The single ampersand can be used to reference the pointer address of a pointer:int* pointer;int* anpointer;anpointer = &pointer;This example, although perhaps not valid, shows that the anpointer reference is now the same as the reference to the pointer memory address.The single ampersand can also be used to reference an object in a function:void function( int param1, int param2, int &reference );If this function were to be called, and the reference object altered within the function, the actualy object that was passed into the function would be altered.The double ampersand '&&' specifies that the left AND the right concepts must both be true before the whole statement is true. For example:if( conceptA true ){conceptC = true;}

In C++ (C Plus Plus), when you call by reference, you are directly accessing the data of the referenced object. When you pass an object to a function by reference, any and all alterations to the object made within the function carry through to the actual object.

A reference is an alias for a variable. Unlike a pointer variable, which must occupy memory in order to store the memory address of the object it points to, a reference consumes no memory other than that used by the object it references. An array name is a typical example of a reference. It is an alias to the memory allocated to the array.

An alias is a reference, an alternate name for a variable or constant. You can assign the address of any variable or constant to a reference of the same type. A reference is a bit like a constant pointer to the type but, unlike a pointer, a reference has no address of its own thus you cannot store references. More importantly, references can never be NULL. They are simply an alternative name by which you can refer to an existing variable or constant. When you assign a value to an existing reference to a variable, you are assigning the value to the variable itself. When you pass a reference to a function, you are passing the address of the value being referred to, and that address is assigned to the function's reference argument and is local to the function. This is not unlike passing a pointer, but pointers may be NULL, references are guaranteed to be non-NULL (a NULL reference invalidates your program). Note that C++ references are not the same as C reference variables or constants. In C, a reference variable is simply a non-const pointer, while a reference constant is a constant pointer. Hence pointers can be dereferenced (both in C and C++). But in C++, a reference is neither a variable nor a pointer, but is constant (it always refers to the same object and cannot be reassigned once assigned).

Parameters are the formal arguments of a function, as defined by the function. When you pass arguments to a function, those arguments are assigned to the function's parameters, either by value or by reference, depending on how the parameters are declared in the function. The following example explains both: void foo( int param ) { // param is a by value parameter, which is a copy of the argument passed to it. } void bar( int& param ) { // param is a reference parameter, which references the argument passed to it. } int main() { int arg = 100; foo( arg ); bar( arg ); return( 0 ); } Note that passing a pointer is the same as passing an int by value: the pointer's value is passed to the function, not the pointer itself. To pass a pointer by reference, you must pass a pointer to pointer and the function's parameter must accept a pointer to pointer.

A pointer is a variable that can store a reference (a memory address) or can store a NULL value (zero). The only use of any pointer in C++ is whenever you need to refer to memory that, for whatever reason, may not be valid. That is, if there is any possibility that a reference could be NULL, then you must use a pointer, because NULL references are invalid in both C and C++.By way of an example, when you dynamically instantiate a new object at runtime, there is no way to guarantee that a sufficient block of free memory will be available, therefore you cannot reference that memory. For that reason, the new operator returns a pointer, not a reference. This is no different to the way the malloc() function returns a pointer, not a reference, in C.As well as dynamic allocations, pointers can also be used to pass objects to functions. While programmers are encouraged to pass objects by reference rather than by value, it is sometimes desirable to pass a NULL reference. But since references can never be NULL, a NULL pointer must be passed instead.Since pointers are variables they are more flexible than references insofar as once a memory address is assigned to a reference, that reference cannot refer to any other memory address while it remains in scope. But pointers can be assigned to point at any reference, including a NULL reference, at any time.

if (*&a > *&b) *&max = *&a; else *&max = *&b;

There are no disadvantages of using pointers in C or C++. There are only poor programmers that write code that incorrectly uses pointers.Its like anything else. You need to consider the meaning and use of a pointer, and only use it in its correct context and initialization.It is true that pointers are a sensitive area, causing problems for many programmers, but the rules are the rules, and if you follow them you can write stable code using pointers.Never use a pointer without initializing it by a call to an allocator.Never use a pointer without checking to see if its initialization failed.Never use a pointer beyond the bounds of its allocation.Always deallocate a pointer after its use has ended.Never use a pointer after it has been deallocated.

An uninitialized pointer is the same as any other uninitialized variable. It has no definite value. Using (or dereferencing) an uninitialized pointer can result in bad results or damage to some unknown area of the program, data, or operating system, or it can result in a bus fault.

You cannot store references. A reference is nothing more than an alias, an alternate name for an existing variable or constant. References are primarily used when passing variables to functions such that the function can operate upon the variable itself -- known as passing by reference. The function refers to the variable by a different name, an alias, but it is the same variable. By contrast, when passing a variable by value the function uses a copy of that variable, assigning the variable's value to that copy. References are often confused with pointers, primarily because C uses the term to mean a pointer (hence the term, dereferencing). But in C++ a reference is a separate entity altogether. Unlike a reference, a pointer is a variable in its own right, one that can be used to store a memory address. Since a pointer has storage, you can store a pointer in a data file. However, in reality you are only storing the pointer's value -- a memory address -- not an actual pointer. Pointers and references are similar insofar as they can both refer to an object. A pointer does this by storing the memory address of the object, while a reference refers directly to the object itself. Thus if you have a pointer and a reference to the same object, the pointer's value is exactly the same as the address of the reference. Therefore the only way you can store a reference is by storing the object being referred to, not the reference itself.

You access memory with a pointer or a reference to the memory. To allocate memory dynamically, use calloc or malloc (C or C++) or new (C++ only).

You call a constructor automatically whenever you instantiate an object, either by declaring a reference to the object type, or by using the new keyword to create a pointer to an object type.Some examples:// Instantiate a reference to an instance of CString:CString myRef; // Calls the default constructor.myRef = "Hello world"; // Calls the assignment operator.// Declare a pointer to a CString object:CString *myPointer; // Does not call constructor; no instance is created.// Point to an existing instance of CString:myPointer = &myRef; // Does not call constructor; instance already exists.// Copy an instance of CString:myPointer = new CString( myRef ); // Calls copy constructor.delete( myPointer); // Call destructor.// Point to a new instance of CString:myPointer = new CString(); // Call default constructor.*myPointer = myRef; // Call the assignment operator.delete( myPointer ); // Call destructor.

A pointer is an object that contains the address of another object, hence the term pointer, meaning that one object points to another.

A pointer is an object that refers to another object. It contains the address of another object. Using dereference syntax (*) you can treat a pointer as an identifier to the object that it points to. Also, you can implicitly treat pointers and arrays in a similar fashion, because array syntax is simply a means of adding an offset to the value of a pointer, before accessing the object to which it points.

A double pointer is a pointer which points to another pointer which points to an object.For example:Foobar** foobar = NULL;This means that somewhere in memory, the class foobar has been created. The variable 'foobar' now points to a pointer to that memory. However, in this example, the pointer is created as 0.

Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.