Swap call by reference confusion


 
Thread Tools Search this Thread
Top Forums Programming Swap call by reference confusion
# 1  
Old 01-20-2014
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.
Code:
#include<stdio.h>

void swap(int *p, int *q)  //Line 3
{
    int tmp;
    tmp = *p; 
    *p = *q; 
    *q = tmp;
}

int main()
{
    int i = 3, j = 5;
    int *iptr, *jptr;
    iptr = &i; 
    jptr = &j; 
    printf("Before swap: i = %d, j = %d\n", i, j); 

    swap(iptr, jptr);   //Line 19
    printf("After swap: i = %d, j = %d\n", i, j); 

    return 0;
}

I know swap(*iptr, *jptr) is wrong when I tried it,
Code:
$ gcc -o pg252b  pg252_call-by_ref01b.c
pg252_call-by_ref01b.c: In function ‘main’:
pg252_call-by_ref01b.c:19:5: warning: passing argument 1 of ‘swap’ makes  pointer from integer without a cast [enabled by default]
pg252_call-by_ref01b.c:3:6: note: expected ‘int *’ but argument is of type ‘int’
pg252_call-by_ref01b.c:19:5: warning: passing argument 2 of ‘swap’ makes  pointer from integer without a cast [enabled by default]
pg252_call-by_ref01b.c:3:6: note: expected ‘int *’ but argument is of type ‘int’

My question is, as the function prototype at Line 3 is swap(int *i, int *j), in which the two arguments are pointers, whereas Line 19 passes two pointer addresses, which are "NOT" matching. Initially I tend to use swap(*iptr, *jptr) instead of swap (iptr, jptr), but not working. I did not see people ask why the function call uses addresses but the prototype declares with pointers. This gave me confusion. Could you guys explain it along with the warning message? Thanks a lot!

Last edited by yifangt; 01-20-2014 at 04:26 PM..
# 2  
Old 01-20-2014
Quote:
My question is, as the function prototype at Line 3 is swap(int *i, int *j), in which the two arguments are pointers, whereas Line 19 passes two pointer addresses, which are "NOT" matching.
Why would int * not match int *?

There's no such distinction between "pointers" and "pointer addresses", I don't know where you got this idea -- could you illustrate your thinking further?

There is a distinction between constant and non-constant pointers, but they're both still pointers -- the difference is what the compiler will allow you to do with them. For instance,
Code:
int a[32];
a++;

You're not allowed to modify the base address of an array the same way you could an 'int *' variable, but 'a' is still a pointer when used in a statement.

Last edited by Corona688; 01-20-2014 at 04:28 PM..
# 3  
Old 01-20-2014
If there is no such distinction, how come swap(*iptr, *jptr) does not work when I used it for Line 19?
My bad to use this expression
Quote:
"pointers" and "pointer addresses"
Here is a reference: On page 253 of the book "A book on C, by Al Kelly, Ira Pohl 4th Edition":
Code:
The effect of "call-by-reference" is accomplished by:
1) Declaring a function parameter to be a pointer;
2) Using the dereferenced pointer in the function body;
3) Passing an address as an argument when the function is called;

To me this is another rule of C that "This is the way to do the job"---not following logic. But, I know it is related to the pointer manipulation again.
My thinking is the "appearance" of the function prototype swap(int *, int *) and the call of the function is swap(&i, &j) that look "not" matching in form.

Last edited by yifangt; 01-20-2014 at 04:53 PM..
# 4  
Old 01-20-2014
Quote:
Originally Posted by yifangt
If there is no such distinction, how come swap(*iptr, *jptr) does not work when I used it for Line 19?
(iptr) is already an 'int *'. Putting a * in front of it dereferences it. This converts it back into an int.

Quote:
My thinking is the "appearance" of the function prototype swap(int *, int *) and the call of the function is swap(&i, &j) that look "not" matching in format.
It might look funny to you but is entirely correct.

Just think about what & and * do as operators. & converts a variable into an address, i.e. converts an "int" into an "int *.

* converts an address into a variable, i.e. converts an "int *" into an "int".

Last edited by Corona688; 01-20-2014 at 05:12 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-20-2014
Quote:
It might look funny to you but is entirely correct.
No, it does not look funny, but my thinking. I must have missed important things with pointers in C! And I will post more silly questions here for help. Thank you very much!
# 6  
Old 01-20-2014
Let me try explaining it a different way:
The function declaration for swap:
Code:
void swap(int *p, int *q)
{ ... }

says that swap() takes two arguments of type pointer to int and does not return a value. So with the declarations:
Code:
    int i = 3, j = 5;
    int *iptr, *jptr;

and the statements:
Code:
    iptr = &i; 
    jptr = &j;

valid calls to swap() would include:
Code:
    swap(&i, &j);
      and
    swap(iptr, jptr);

because &i, &j, iptr, and jptr are all of type pointer to int. The call:
Code:
    swap(*iptr, *jptr);

is not valid because *iptr and *jptr (which can be read as "the int pointed to by the pointers to int iptr and jptr, respectively) are objects of type int; not objects of type pointer to int.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-21-2014
Thanks Don!
While reading the post I was still thinking: about call-by-reference of previous post.
Quote:
On page 253 of the book "A book on C, by Al Kelly, Ira Pohl 4th Edition":
Code:
The effect of "call-by-reference" is accomplished by:
1) Declaring a function parameter to be a pointer;
2) Using the dereferenced pointer in the function body;
3) Passing an address as an argument when the function is called
;
I think I have to remember these rules. Declare with pointers but call with address. Here "pointer" and "address" are the same in fact, just look different in form, and remember to add the "&" symbol for non-pointer variables, or without "*" for pointers including function/struct.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies

2. 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

3. UNIX for Dummies Questions & Answers

[Query] Confusion of the Swap when using 'free -m' command

Hi All, I have just installed my first Linux server ( Ubuntu 11.10 ). I am sure I didn't allocate /swap , and double check by 'df -h', yes really no /swap but when I use 'free -m' , returned a "Swap" line as below. total used free shared buffers cached Mem: ... (3 Replies)
Discussion started by: joaming
3 Replies

4. Programming

call by reference

Ive just started programming in C and am confused with the output of the 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... (3 Replies)
Discussion started by: ra2000
3 Replies

5. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 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. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies

8. Solaris

RAM and SWAP confusion

Hi Folks, This is my first post here - so nice to meet u all :-) Recently i was trying to dig a little bit into the memory structure of my machine and due to the lack of concept, cannot figure out a calculation. This is how it goes: 1) My swap slice is at the usual /dev/dsk/c0t1d0s1... (0 Replies)
Discussion started by: s4g3
0 Replies
Login or Register to Ask a Question