Take BSc Tuition from the Best Tutors
Search in
Answered on 16/03/2019 Learn BSc Computer Science
Ruhinaz
Tutor for all studying n needing students
Clear the basic studies already done in 1st n 2nd year of bsc subject then go through the syllabus and note down the things u know then u cann make ur own notes and google will help u in that as per a tutor take help of best IT tutor for hard topics who can make you understand better than 1st
read lessLesson Posted on 23/12/2017 Learn BSc Computer Science
C++: Passing A Function As an Argument
Ashutosh Singh
Subject matter expert (Computer Science & Engineering) at Chegg India since June 2019. Teaching programming...
#include
using namespace std;
int sum(int a,int b)
{
return a+b;
}
void f2(int (*f)(int,int),int a,int b) //'*f' is a POINTER TO A FUNCTION whose RETURN TYPE is //'int' and arg type (int,int) . Here, '*f' is FORMAL PARAMETER
{
cout<<sum(a,b)<<endl;
}
int main()
{ int (*p1)(int,int)=∑ //Here, '*pf1' is ACTUAL PARAMETER
f2(sum,10,20); //NAME OF A FUNCTION IS ITSELF A POINTER TO ITSELF
f2(p1,20,79);
return 0;
}
Lesson Posted on 09/11/2017 Learn BSc Computer Science
GCC
Java 9 is here! A major feature release in the Java Platform Standard Edition is Java 9
Lets see what more it offers more than its previous versions
Moreover it includes enhancements for microsoft windows and MAcOS OS platforms
read less
Take BSc Tuition from the Best Tutors
Lesson Posted on 23/08/2017 Learn BSc Computer Science
SR-IT Academy
SR - IT Academy is one of the leading tutorial point providing services like tutoring and computer training...
while (left <= right)
The loop invariant is:
all items in A[low] to A[left-1] are <= the pivot
all items in A[right+1] to A[high] are >= the pivot
Each time around the loop:
left is incremented until it "points" to a value > the pivot
right is decremented until it "points" to a value < the pivot
if left and right have not crossed each other,
then swap the items they "point" to.
Lesson Posted on 07/08/2017 Learn BSc Computer Science
Deadlocks In Distributed Systems
SR-IT Academy
SR - IT Academy is one of the leading tutorial point providing services like tutoring and computer training...
Lesson Posted on 05/07/2017 Learn BSc Computer Science
What Are The Two Forms Of #Include?
Shiladitya Munshi
Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...
Take BSc Tuition from the Best Tutors
Lesson Posted on 05/07/2017 Learn BSc Computer Science
What Is the Difference Between Function And Subroutine?
Shiladitya Munshi
Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...
Lesson Posted on 05/07/2017 Learn BSc Computer Science
Shiladitya Munshi
Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...
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
#include
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
#include
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);
foo(&i);
printf("\n after calling foo() the value of the variable: %d",i);
getch();
return 0;
}
Your code will show the result as
Your points are well taken. But the thing is what you are showing is not at all calling a function by reference. It just the function call by value! Here you are essentially copying the value of address of your variable i and calling the function foo with that copy. Now eventually in this case, the value that is being passed contains the address of another variable. Within the function, you are accepting this value with a pointer and changing the value of the content addressed by that pointer. So it is nothing but a function call by value only.
Please note that to change the value of the content addressed by a pointer, you are to use *, no way could it be thought of as a reference.
Now let me give you one example of true function call by reference
#include
#include
void foo (int& r1)
{
r1 = 5;
printf("\n Inside foo() the value of the variable: %d", r1);
}
int main(void)
{
int i = 10;
int &r = i;
printf("\n before calling foo() the value of the variable: %d",i);
foo(r);
printf("\n after calling foo() the value of the variable: %d",i);
getch();
return 0;
}
Will this run with your C compiler? No.
Note:: I have used DevC++ as the coding platform
read less
Lesson Posted on 05/07/2017 Learn BSc Computer Science
Some Interview Questions And Answers For Fresher Level On Pointers
Shiladitya Munshi
Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...
Take BSc Tuition from the Best Tutors
Answered on 22/03/2017 Learn BSc Computer Science
Kousalya Pappu
Tutor
UrbanPro.com helps you to connect with the best BSc Tuition in India. Post Your Requirement today and get connected.
Ask a Question
The best tutors for BSc Tuition Classes are on UrbanPro
The best Tutors for BSc Tuition Classes are on UrbanPro