call by reference


 
Thread Tools Search this Thread
Top Forums Programming call by reference
# 1  
Old 01-30-2011
call by reference

Ive just started programming in C and am confused with the output of the code

Code:
void main()
{
    int a=10;
     int b=20;
    swapr(a++,b++);
    printf("%d %d",a,b);
}
 swapr (int *x,int *y)
{
    x+=2;
    y+=3;
}

the output of the code is 11,21 which is confusing. first its call by reference but the values of a and b are not swapped and second, the values of a and b are not incremented by 2 and 3 respectively.
please help with the reasoning.

Last edited by pludi; 01-31-2011 at 06:04 AM..
# 2  
Old 01-30-2011
what compiler do you use?didn't it generate any error or warning?I modified your code as below:
Code:
void swapr (int *x,int *y);

//shoule be return int,your style is old-fashioned
int main()
{
	int a=10;
	int b=20;
	//swapr needs two parameters of int *,it's a address.
	//C's reference means to use pointer.
	swapr(&a,&b);
	printf("%d %d",a,b);
}

//also,return value
void swapr (int *x,int *y)
{	
	//if you wanna change the values of x and y
	*x+=2;
	*y+=3;
}

more,<Pointers on C> is a good book to learn C.
# 3  
Old 02-07-2011
Code:
#include <iostream.h>
void swapr (int * x,int * y);
//shoule be return int,your style is old-fashioned
int main()
{
    int a=10;
    int b=20;
    //swapr needs two parameters of int *,it's a address.
    //C's reference means to use pointer.
    swapr(&a , &b);
    cout <<a << "   "  << b;
    return 0;
}
//also,return value
void swapr (int * a,int * b)
{
 
//just fr your swapr to work
      * a= * a + * b;
      * b= * a - * b;
      * a= * a - * b;
}


Last edited by radoulov; 02-07-2011 at 01:18 PM.. Reason: Code tags, please!
# 4  
Old 02-07-2011
Quote:
Originally Posted by vineetjoshi
//shoule be return int,your style is old-fashioned
Not so much "old fashioned" as "was always wrong"... One popular book apparently spread the "void main" usage decades ago and even now we're still struggling to kill it.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get last reference date

Hi, Could you please help me to get last reference date in Unix, in Unix we maintain SAS7BDAT files. Is there any command or script to get the info, Thank you. (2 Replies)
Discussion started by: subbarao12
2 Replies

2. Programming

Swap call by reference confusion

Hello, This is very silly question as millions discussions on call-by-value vs call-by-reference for programming beginners, but I need to confirm my understanding. #include<stdio.h> void swap(int *p, int *q) //Line 3 { int tmp; tmp = *p; *p = *q; *q = tmp; } int... (15 Replies)
Discussion started by: yifangt
15 Replies

3. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

4. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem is a function which i typed although it kept saying that it is a undefined reference still. other... (1 Reply)
Discussion started by: mgyeah
1 Replies

5. UNIX for Dummies Questions & Answers

Why do I need to call make if I call gcc ?

Why do I need to call make if I call gcc ? I thought gcc already compiles the sources. thanks (1 Reply)
Discussion started by: aneuryzma
1 Replies

6. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

7. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies
Login or Register to Ask a Question