Computer Programming
C Programming
The Difference Between

What is the difference between a reference and a pointer in c?


Top Answer
User Avatar
Wiki User
2007-06-20 13:17:09
2007-06-20 13:17:09

The difference that i learnt very recently and the one i remember :-)

Reference cannot be changed whereas pointer value can be changed.

Actually, const pointer = reference.


Related Questions

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

pointer: to access data by address reference: there is no reference in C language

There is no similarity between the two.

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.

--> Array is a collection of same kind of data.--> Pointer is a memory location where the data stored in the memory

A pointer is a reference to some chunk of memory on your computer. To actually get the value out of said chunk of memory, you must dereference it (i.e. *pointer).

On the lower level of Java, a "reference" can be thought of like a pointer in C. It is essentially an integer which refers to (points to) a location in memory where the object data exists. // "button" is a reference to a JButton with a "1" on it (the object). JButton button = new JButton("1");

Reference VariableIt does not have separate memory other than variableIt is just an alias created to existing variableIt is not present C languagePointer VariableIt has separate memory other than variableIt actually stores address of the variableIt is present in C as well as C++.

Example: 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

The C language does not support references, that is, the C++ concept of creating an alias to a variable. You can create pointers and dereference them, or you can use the preprocessor's #define mechanism to use another name for a variable. Even in C++, however, you cannot create a pointer to a reference. If you try to, you will end up creating a pointer to the original value. This is because all a reference is is an automatically dereferenced pointer - a compiler shortcut, rather than a totally new feature. So the limitation of not being able to point at a reference isn't really too bad.

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).

When you pass a parameter by value, you are creating a copy of the original variable and the receiving (method/function) cannot change the original value. When you pass by reference you are passing a pointer to the original object and therefore any changes are made to the original value.

In .NET: Both are passed by pointer. However, call by value the actual value (caller value) passed will not change. Call by reference the actual value (caller value) passed will change.

Pass by value and pass by reference apply to functions that accept formal arguments. A formal argument may be a value or a reference. The function's caller uses actual arguments and the calling convention determines how an actual argument is assigned to the corresponding formal argument. If the formal argument is a value, then the value of the actual argument is assigned to the formal argument. If the formal argument is a reference, then the address of the actual argument is assigned to the formal argument. The nett effect is that when you pass by reference, both the formal and actual arguments refer to the same object. Whereas when you pass by value, the formal argument is a copy of the actual argument. Some languages, like C, do not support the notion of a reference argument and only use pass by value semantics. However, C does support the notion of a pointer data type which is a variable that can store a memory address. The value of a pointer is therefore a reference and passing a pointer by value is the same as passing by reference. This is because when a pointer value is assigned to another pointer, the address (the value of the pointer) is copied, thus the two pointers refer to the same object. C++ evolved from C and uses the same pass by value semantic by default. However, C++ also has a dedicated reference type that allows native pass by reference semantics. A reference differs from a pointer in that a pointer is a variable and therefore requires memory of its own to store the address it refers to. Thus a pointer has an address and a value, just like any other variable. But a reference does not have its own address because a reference is simply an alias, an alternate name for an object in memory. In other words, the address of a reference is the address of the object it actually refers to, in the same way that Billy could be alternate name for an object named William; they are one and the same object by different names. A pointer is not an alias, it is a completely separate object. Another key difference is that a reference cannot be reassigned, it must be initialised at the point of declaration. In this respect it is similar to the behaviour of constant pointer. Finally, a reference can never refer to a non-existent object, whereas a pointer can. When a pointer is no longer required it must be nullified by assigning it the value zero.

The final keyword in Java has more in common with a C++ reference than it does with a C++ const. That is, a reference may only be assigned once, but the referenced object may still be modified. A C++ reference is the same as a constant pointer (rather than a pointer to const), insofar as the pointer cannot be modified, but the memory being pointed to can. This emulates some of the behaviour of the Java final keyword. But while a C++ const can easily be cast to a volatile, you simply cannot do that in Java. Moreover, a Java final class cannot be sub-classed, you simply cannot do that in C++. Comparing Java final to C++ const is somewhat pointless since they have absolutely nothing whatsoever in common; the list of differences could fill a book.

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.

Indirect addressing uses a pointer. Indirectly accessing the memory being pointed at is known as dereferencing. Direct addressing uses a variable's name or a reference to obtain the value.

Pointer in C are fun to programming.Pointers in C are variables which doesn't stores the value but points to the address where the value is stored.They are used for reference.

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.)

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.

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).

In a pass by pointer (*, the original C standard), you're not passing the actual variable but rather its address in memory. This is required because in standard C, all function parameters are passed by value and therefore disappear when the function call is completed. In order to access and alter the original variable, you have to dereference the pointer using the address provided in the pointer argument, and this must be done each time the original variable is altered. Keeping this in mind requires vigilance to be sure you know which object referring: the variable or its memory address.In a pass by reference (&, introduced in C++), you're telling the compiler that the variable being passed into a function has to be fundamentally linked to the original. The compiler takes care of the dirty work and lets you refer to the variable with simple assignment statements--no pointer handling required.

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;}

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.

Not to be pedantic, but you cannot call a destructor explicitly. Destructors are implicitly called when an object falls from scope or when you delete a pointer to an object. Any object created dynamically (with the new keyword) must be deleted as soon as you are finished with it, and before the pointer falls from scope. In this sense, you are explicitly calling the object's destructor, however it's really being called implicitly by you deleting the pointer. It's also good practice to explicitly NULL your pointer immediately after deleting the object it pointed to. An object reference is destroyed automatically when the reference falls from scope. If you have a pointer to that reference, do not delete the pointer, but do assign it to NULL as soon as possible to prevent any access to the deleted object. If you do delete a pointer to a reference that's still in scope, you will render the reference NULL and a NULL reference will render your program invalid.

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.