LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-28-2012, 07:27 AM   #1
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
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()??
 
Old 06-28-2012, 07:38 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,719

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
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);
 
Old 06-28-2012, 07:51 AM   #3
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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;
}
 
Old 06-28-2012, 09:16 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,719

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
Yes, that's how call-by-reference is different to call-by-value.
 
1 members found this post helpful.
  


Reply

Tags
pointer


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 03:35 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM


All times are GMT -5. The time now is 06:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration