LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 10-19-2004, 02:01 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
C and "call by reference"


I will explain my question through an example:
void swap(int *a, int *b)
void swap(int &a, int &b)
Question 1: are both of these techniques (the first technique using pointers and the second using addresses) called "call by reference"?
Question 2: both of these have the same result when they are used; the first one uses addresses as arguments and the second one pointers.
Which is their difference? Which one is faster? Which one uses more memory space? When should i use the first technique and when should i use the second one?
 
Old 10-19-2004, 02:08 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
C has no such thing as "pass by reference". You can only pass values to functions in C. void swap(int &a, int &b); is not valid C code. In C you would do something like this:
Code:
void swap(int *a, int *b)
{
  int temp;

  temp = *a;
  *a = *b;
  *b = temp;
}

int main(void)
{
  int a = 5, b = 7;

  printf("a = %d, b = %d\n", a, b);
  swap(&a, &b);
  printf("a = %d, b = %d\n", a, b);

  return 0;
}
swap(&a, &b); may look like you're passing by reference, but you're really passing a value; the address of the variable.

Last edited by itsme86; 10-19-2004 at 02:10 PM.
 
Old 10-19-2004, 02:10 PM   #3
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
thanks
 
Old 10-20-2004, 12:10 AM   #4
SoulSkorpion
LQ Newbie
 
Registered: Oct 2004
Distribution: Knoppix
Posts: 6

Rep: Reputation: 0
That's not the whole story.

C++ allows passing by reference, using the syntax you've described. Note: C++, NOT C. Don't confuse the two. Enough people do as it is...

itsme86 demonstrated the C way. C++ allows you to do it like this:

Code:
void swap(int& a, int& b)
{
  int temp;

  temp = a;
  a = b;
  b = temp;
}

int main() //no void in the declaration, for it is Bad and Vile.
{
  int a = 5, b = 7;

  printf("a = %d, b = %d\n", a, b);
  swap(a, b);
  printf("a = %d, b = %d\n", a, b);

  //no return code necessary
}

Last edited by SoulSkorpion; 10-20-2004 at 06:34 AM.
 
Old 10-20-2004, 04:34 AM   #5
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
????????????????
??????????????
?
 
Old 10-20-2004, 05:44 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Passing using * and & do the same thing internally; they pass a pointer. The use of & is a semantical convenience allowed by the compiler so that you don't have to do *a = or a-> to use the argument; it makes the pointer appear like an ordinary object. Another reason to use a reference is so you don't have to check for a bad pointer in the function: even though the reference operation actually passes a pointer, it must be a valid object (unless you dereference a bad pointer as the argument.) This means that NULL cannot be passed as would be allowed by a pointer argument. The bad thing is temp objects can't be used; if your function asks for int&, you cannot give it a char argument; even though it converts if by value, the references cannot be converted. Therefore; do not use for POD unless you are changing the value.

Pass by reference merely means that the argument is not copied. A * argument is actually a pointer, which is a value, therefore that is a pointer argument being passed by value. You can, however pass a pointer by reference with *&, thereby allowing you to change the pointer value, to say a new[] array. You can have multiple pointer types e.g. int**, but you can only use one reference per type, and cannot take a pointer to a reference; int*& OK int**& OK int&* NOT OK int&& NOT OK.
ta0kira
 
Old 10-20-2004, 06:34 AM   #7
SoulSkorpion
LQ Newbie
 
Registered: Oct 2004
Distribution: Knoppix
Posts: 6

Rep: Reputation: 0
Quote:
Originally posted by nocturna_gr
????????????????
??????????????
?
Whoops. Sorry, mentioned the wrong person. I've edited my original post.
 
  


Reply



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
"undefined reference"linker error for static field w/ C++ astorm Programming 5 08-27-2008 03:00 AM
Comments request for "O'Reilly's Linux iptables Pocket Reference" carboncopy General 3 03-03-2005 09:24 PM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
"Function not implemented" error in call to "sem_open()" Krishnendu8 Linux - Newbie 1 06-07-2003 02:52 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:46 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration