Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-28-2012, 07:27 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2011
Posts: 18
Rep: 
|
output
Code:
#include<stdio.h>
#include<iostream.h>
void fun(int *b)
{
int c=5;
cout<<"value of pointer in fun()"<<"\n"<<b;
b=&c;
(*b)++;
cout<<"the value of c is"<<*b;
}
int main()
{
int a=4;
int *p=&a;
cout<<"previous value of a is "<<*p;
cout<<"value of pointer in main :"<<"\n "<<p;
fun(p);
cout<<"after calling fun ,new value of a is "<<*p;
return 0;}
...
how is the o/p remaining the same in main() even after we change the pointer's to point to another variable in func()??
|
|
|
|
06-28-2012, 07:38 AM
|
#2
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,047
|
The parameter is passed by value, so this statement: 'b=&c' changes only the copied value, not the original.
PS: you really should use printf, it is much more flexible, eg:
Code:
printf ("main, beforore 'fun': a=%d, p=%p\n", a, p);
|
|
|
|
06-28-2012, 07:51 AM
|
#3
|
|
LQ Newbie
Registered: Jun 2011
Posts: 18
Original Poster
Rep: 
|
Quote:
Originally Posted by NevemTeve
The parameter is passed by value, so this statement: 'b=&c' changes only the copied value, not the original.
PS: you really should use printf, it is much more flexible, eg:
Code:
printf ("main, beforore 'fun': a=%d, p=%p\n", a, p);
|
thanx.i thnk i get it..but jst to make sure when i change the argument to be passed as refernce, the o/p changes..so does this actually mean when we pass a reference to a variable,another copy of that reference is not created in func()??
Code:
#include<iostream.h>
void fun(int &b)
{
int c=5;
b=c;
b++;
}
int main()
{
int a=4;
int &p=a;
cout<<"previous value of a is "<<p;
fun(p);
cout<<"after calling fun ,new value of a is "<<p;
return 0;
}
|
|
|
|
06-28-2012, 09:16 AM
|
#4
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,047
|
Yes, that's how call-by-reference is different to call-by-value.
|
|
|
1 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|