answersLogoWhite

0

It is by-value, actually, not by-reference.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How are the strings passed to a function?

By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)


Is call by reference supported by pointer?

No, call-by-reference can be emulated with pointers.


What is parameter passing by value and by reference to a function?

Pass by value means the value of the actual argument is assigned to the formal argument of the function. This is how all functions work in all languages, but in some languages, such as Java, the implementation details may be hidden from us so it may not be obvious pass by value is occurring. When we say pass by reference what we really mean is that the value being passed is a memory address rather than an ordinary value. That is; the memory address is being passed by value. Memory addresses can only be stored in pointers, so for a function to accept an address its formal argument must be pointer of the same type. In languages that do not support pointers, such as Java, the pointers are still there we just don't have access to them. In C++ we also have a native reference type. A reference is nothing more than an alias, an alternative name for an object of the same type. Unlike a pointer, a reference cannot change which object it refers to; it always refers to the same object it was assigned when it came into scope. Moreover, a reference can never be null. As a result, references are much easier to work with than pointers because we know that an object must exist if we have a reference. With pointers, we must test the pointer is non-null before we can determine if it refers to an actual object. In addition, a pointer has storage and identity whereas a reference does not. If we attempt to take the address of a reference, we take the address of the object being referred to. If we take the address of a pointer we get the pointer's own address (as opposed to the address stored at that address). Although C++ supports a native reference type, behind the scenes the compiler uses pointers to implement them. So references are really just passed by value the same way pointers are passed by value. Regardless, if a function accepts either a pointer or a reference, it is said to be using the pass by reference semantic, otherwise it is using the (default) pass by value semantic.


What is actually passed to a function when an object is passed to a function through a pointer?

When an object is passed to a function by pointer, a copy of the pointer's value is passed to the function. Pointers are always passed by value, but because the value is a memory address than can be dereferenced, they enable us to pass objects by reference. In languages such as C which have no concept of references, this was the only way to pass by reference. C++ introduced proper references (aliases for existing objects), thus when we want to pass by reference we can choose to use a pointer or an actual reference. Normally we'd only pass by pointer when passing an optional argument that defaults to NULL when no argument is given. Otherwise we pass by reference because references can never be NULL.


Why you use pointers?

Because you can produce fast and efficient code. Function arguments are passed "by value", and so you can't change the original value of the argument, but if you use pointers, you can.


What is the uses of call by value in c?

You pass by value when you do not want the original value to change and the value being passed is no more than 4 bytes long (or 8 bytes on a 64-bit system). If the value is greater than 4 bytes, you will incur a copy overhead, which can be expensive if the value is an array or other complex structure or object. If the value is passed as a const, the function will not alter its copy of the value. If the value is passed as non-const, the function can alter its copy of the value. Either way, the original value is unaffected. Pointers are always passed by value. That is, the pointer is copied by the function, leaving the original pointer unaffected. However, since the copied pointer points to the same memory as the original pointer, it's as if you had passed a reference to that memory, without copying that memory. If the pointer is passed as a const, the immutable members of the memory being pointed at will be unaffected by the function call. If it is non-const, it is expected that the memory being pointed at will be changed by the function call. If you do not wish the original memory to be altered, you must copy that memory and pass a pointer to the copy instead. Naturally, this will incur a performance penalty. Passing by reference passes the actual value being referenced, without copying. However, the compiler implements references as pointers so the same rules apply as for pointers. If the reference is non-const and you do not wish the value to be altered, copy the value and pass a reference to the copy instead. Passing a pointer by reference, rather than by value, is achieved by passing a pointer to the pointer, rather than a copy of the pointer. This allows the function to change the value of the original pointer. That is, the original pointer can be altered by the function to point at another memory location. When dealing with dynamic, multi-dimensional arrays, pointers to pointers (to pointers!) are fairly common, if only to avoid the costly copy overhead.


In c plus plus ....Differences in passing primitive types and objects types?

Primitive types are usually passed be value (many professional programmers use reference or pointers). For object types is always used mechanism pass-by-reference because it allows to save a lot of memory by preventing coping data.


What are the advantages of pointers in a program?

A pointer is simply a variable that stores a memory address and that allows that memory to be dereferenced. Unlike a reference, which must always refer to the object assigned to it, a pointer can refer to any object of the pointer's type, including null. A null pointer is simply a pointer that does not refer to any object. In languages that do not support pass by reference (such as C), pointers must be used to pass objects to functions. The pointer is passed by value, but the value is a memory address, which is the same as passing the object at that address. In languages that do support references (such as C++), references are useful when you need to guarantee an object exists because a reference can never be null. However, when passing an object to a function as an optional parameter, you must use a pointer because pointers can be null. Programmers must test pointers to ensure they are non-null before attempting to dereference them. References do not need to be tested; if a reference exists, it must refer to an object in memory.


Why pointers are required?

The most important use of pointers comes when we pass value by reference to any function. You do not need to create a second memory location as in pass by value. You can mofify the original variable by using its address.


What is the advantage of using pointer in c?

Pointer variables have many uses. Primarily, we use pointers to keep track of variables allocated on the heap (the free store). We cannot name these variables because they do not exist at compile time, so we must use pointer variables to store the addresses of these variables as and when they are allocated at runtime. Hence the return value of the C standard library malloc() function is a memory address. We also need to use pointers when passing arrays to and from functions because arrays cannot be passed by value, they can only be passed by reference. More generally, we can use pointers to pass and return any variable by reference, particularly large structures that would be too expensive to copy if passed by value. In C++ we can use native reference types rather than pointers, but in C there is no native reference type, so we must use pointers if we wish to work with memory addresses. We often use the term reference when referring to pointers in C, however there are important distinctions. Unlike a pointer, a reference is not a variable (it is an alias or alternate name for a memory address) and references can never be null. The null address (usually address 0) is a system address which we can assign to a pointer whenever we have no valid address to assign to it. Thus we can also use pointers as function arguments when "no object" may be a valid argument. Pointers and arrays are closely related in C. In some respects, an array name behaves like a reference in C++. That is, the name refers to the start address of the array (the address of the first element of the array). As such, the name and the address of the name are one and the same, as is the address of the first element: int a[100]; assert (a == &a); assert (a == &a[0]); The array subscript operator [] is nothing more than a sugar-coated abstraction to simplify the pointer arithmetic required to navigate an array. For any array a and any integer j within the range of a, we have the following equivalences: a[j] j[a] It often surprises people that a[j] and j[a] are equivalent, however the compiler will interpret j[a] as meaning *(j+a), which is nothing more than trivial pointer arithmetic. Such low-level cleverness does not belong in production code, of course.


Does java support the concepts of multiple inheritance and pointers?

No. Java was designed with programmer friendliness and ease of maintenance of code in mind. Pointers makes code pretty complicated and providing direct access to the memory can have devastating effects in the hand of a mailcious or an inexperienced programmer. Also, direct multiple inheritance is not supported by java. You can achieve partial multiple inheritance with the help of Interfaces.


What is difference between 'call by value' and 'call by reference' in the book-computer in programming language-author -vrajaramani?

With call by value semantics, the value of the variable is passed to the function. With call by reference semantics, the memory address of the variable is passed to the function. Pass by reference is typically used to pass large and complex types to functions because there is no performance penalty in passing objects by reference. Passing by reference also makes it possible for the function to modify the argument. If the function does not need to modify the reference, the reference should be declared constant. This assures the caller that the non-mutable members of the object being passed will remain in an unmodified state. Pass by value should only be used to pass primitive or built-in data types. That is, any type that is small enough to fit into a CPU register. This is because pass by value makes a copy of the variable and copying large or complex variables can be an expensive operation in terms of both performance and memory consumption. Imagine passing an array with 1 million elements, each of which must be copy constructed! Copying a single memory address is as cheap as it gets and that's precisely what pass by reference means. Some languages do not support a native reference type and therefore default to pass by value semantics. C is a typical example. However, C supports native pointer types which are simply variables that store memory addresses. Thus you can enable pass by reference semantics by passing a pointer by value. The pointer itself is copied, not the value being pointed at. Many high-level languages do not support native pointers and therefore default to pass by reference semantics. However, this is achieved through resource handles; there is an underlying pointer being passed (by value) behind the scenes but it is (usually) inaccessible to the programmer. Java is a prime example of this. If you wish to use pass by value semantics you must explicitly pass a copy of the object. Defaulting to pass by reference semantics simply encourages efficient coding. C++ is one of a few languages that supports both pointers and references and either method can be used to enable pass by reference semantics. The main differences between a pointer and a reference are that pointers are variables which may be null whereas references are aliases (alternate names for existing objects) which can never be null. More specifically, the address of a reference is the address of the object being referenced and that object must exist (a null reference is an error). A pointer refers to the address stored in the pointer itself (the value of the pointer) thus the address of a pointer is separate from the address of the object being pointed at. This makes it possible for pointers to refer to other pointers, thus making it possible to pass pointers by reference.