UrbanPro
true

Take BCA Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Pointers and References

Shiladitya Munshi
22/11/2016 0 0
Are reference and pointers same?
 
No.
 
I have seen this confusion crumbling up among the student from the first day. So better clear out this confusion at thevery beginning.
Pointers and reference both hold the address of other variables. Up to this they look similar, but their syntax and further consequences are totally different. Just consider the following pieces of code
 
Code 1                                         Code 2
int i;                                             int i;
int *p = &i;                                 int &r = i ;
 
Here in code 1 we have declared and defined one integer pointer p which points to variable i, that is now , p holds the address of i.
 
In code 2, we have declared and defined one integer reference r which points to variable i, that is now, r holds the address of i. This is completely same as that of p.
 
So where is the difference?
 
The first difference can be found just by looking at the code. Their syntaxes!
 
Secondly the difference will come up when they would be used differently to assign a value (suppose 10) to i.
 
If you are using a pointer, you can do it like *p = 10; but if you are using a reference, you can do it like r = 10.  Just be careful to understand that when you are using pointers, the address must be dereferenced using the *, whereas, when you are using references, the address is dereferenced without using any operators at all.
 
This notion leaves a huge effect as consequences. As the address of the variable is dereferenced by * operator, while using a pointer, you are free to do any arithmetic operations on it. That is you can increment the pointer p to point to the next address just by doing p++. But, this is not possible using references.  So a pointer can point to many different elements during its lifetime; where as a reference can refer to only one element during its life time.
 
 
Does C language support references?
 
No. The concept of reference has been added to C++, not in C. So if you run the following code, C compiler will object then and there.
 
#include<stdio.h>
#include<conio.h>
int main(void)
{
    int i;
    int &r = i;
    r = 10;
    printf("\n Value of i assigned with reference r = %d",i);
    getch();
    return 0;
}
 
But if you are using any C++ compiler, this code will work fine as expected.
 
If there is no concept of reference in C language, then how come there exists C function call by reference?
 
Strictly speaking, there is no concept of function call by reference in C language. C only supports function call by value. Though in some books ( I will not name any one) it is written that C supports function call by reference or the simulation of  function call by reference can be achieved through pointers, I will strongly say that C language neither directly supports function call by reference, nor provides any other mechanism to simulate the same effect.
 
I know you are at your toes to argue that what about calling a C function with address of a variable and receiving it with a pointer? The change made to that variable within the function has a global effect. How this cannot be treated as an example of function call by reference?
 
You probably argue with a code like following
 
#include<stdio.h>
#include<conio.h>
void foo(int* p)
{
     *p = 5;
      printf("\n Inside foo() the value of the variable: %d",*p);
}
int main(void)
{
    int i = 10;
    printf("\n before calling  foo() the value of the variable: %d",i);
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

What Is the Difference Between Function And Subroutine?
The difference is indeed very thin and it depends on the context. In general Function is a piece of code with a name, inputs (arguments) and an output (return types). That is function should always return....

Falling Sphere Viscometer
This viscometer is covered in BS188 and is based on measuring the time for a small sphere to fall in a viscous fluid from one level to another. The buoyant weight of the sphere is balanced by the fluid...

C, Cpp, Java, Python, Javascript Programming Languages
Learning C is as good as Learning a Natural Language(such as English, Hindi, Telugu, Tamil etc) We use Natural Languages to communicate with humans whereas we use Programming Languages such as C, Cpp,...
V

Vamshi

0 0
0

Default Methods in Java 8
Java 8 was released in early 2014 (18th March 2014). Java 8 is a product version. It's development version is JDK1.8 ( Java Development Kit ). Java 8 has some new features that are most awaited in the...
A

Arundhati Pathak

1 0
0

An overview
When a gas is heated, change takes place in (A) Temperature (B) Pressure (C) Volume (D) All of these Correct Answer One molecule of oxygen consists of __________ atoms of oxygen. (A)...
X

Looking for BCA Tuition Classes?

The best tutors for BCA Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BCA Tuition with the Best Tutors

The best Tutors for BCA Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more