passing by refrence isn't allowed in C?

This is a discussion on passing by refrence isn't allowed in C? within the C Programming forums, part of the General Programming Boards category; I was looking at my old C++ book and it said one of the differences between C and C++ was ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    passing by refrence isn't allowed in C?

    I was looking at my old C++ book and it said one of the differences between C and C++ was that in C you couldn't pass by reference (I always did by pointers, so it didn't apply to me fully) but I tried it out and I was surprised. How come this works on C++ but not C?

    Code:
    #include <stdio.h>
    
    int blah(int &a, int &b)
    {
    	int c;
    	c = a;
    	a = b;
    	b = c;
    	return 0;
    }
    
    int main()
    {
    	int a[2];
    	a[0] = 34;
    	a[1] = 32;
    	printf("a = %d\nb = %d",a[0],a[1]);
    	blah(a[0],a[1]);
    	printf("a = %d\nb = %d",a[0],a[1]);
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How come this works on C++ but not C?
    Because that's the rules of the languages. C has no pass by reference, C++ does.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    yeah, well I figured out something.
    Swap in C just became more difficult tho.
    I wanted to do something like:
    Code:
    int swap(int* a, int* b)
    {
      int* c = a;
      a = b;
      b = c;
    }
    but it didn't work the way I wanted it to.

    So instead Ill just stick to type to this:
    Code:
    void swap(int *a, int *b)
    {
    	*a ^= *b;
    	*b ^= *a;
    	*a ^= *b;
    	return;
    }
    oh well...

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int swap(int* a, int* b)
    {
      int* c = a;
      a = b;
      b = c;
    }
    Instead try this.
    Code:
    void swap(int* a, int* b)
    {
      int c = *a;
      *a = *b;
      *b = c;
    }

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I also tried that but it didn't work.

    Doesn't make sense, but whatever.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Try swoopy's answer again - works for me

    Code:
    #include <stdio.h>
    void swap(int* a, int* b)
    {
      int c = *a;
      *a = *b;
      *b = c;
    }
    int main()
    {
    	int a[2];
    	a[0] = 34;
    	a[1] = 32;
    	printf("a = %d\nb = %d\n",a[0],a[1]);
    	swap(&a[0],&a[1]);
    	printf("a = %d\nb = %d\n",a[0],a[1]);
    	return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    no that works, thats essentially the same solution I had but I wanted to be able to switch the pointer's values (modify the pointers).
    Yeah, that doesn't work. It's ok, I found ways around that hehe.

    Thanks though.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You wouldn't be using references like that anyway if you wanted to swap pointers. You're trying to swap two values, so to reiterate previous posts, it's
    Code:
    >> int temp = *a;
    >> *a = *b;
    >> *b = *temp
    be careful, too- xor'ing a value by itself results in zero.
    I think that using an old trick to save a single integer from being created is silly, too. The other way is more readable anyway.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ur method doesn't work

    temp isn't a pointer
    you can't derefrence it.

    and I know about the XOR thing, but I make sure that whatever is swapped points to different things. if it didn't i wouldn't swap it. Saves memory (what little there is to be saved anyway)

    -LC
    Last edited by Lynux-Penguin; 10-05-2003 at 02:50 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Lynux-Penguin
    no that works, thats essentially the same solution I had but I wanted to be able to switch the pointer's values (modify the pointers).
    Yeah, that doesn't work. It's ok, I found ways around that hehe.

    Thanks though.

    -LC
    To swap the pointers around, use something like this:
    Code:
    #include <stdio.h>
    
    void swap (int **a, int **b)
    {
      int *c = *a;
      *a = *b;
      *b = c;
    }
    
    int main(void)
    {
      int i = 1;
      int j = 2;
      int *ip = &i;
      int *jp = &j;
      
      printf ("ip points to %p, which matches i (%p).  \nThe value at the end of the pointer is %d\n", (void*)ip, (void*)&i, *ip);
      printf ("\njp points to %p, which matches j (%p).  \nThe value at the end of the pointer is %d\n", (void*)jp, (void*)&j, *jp);
    
      swap (&ip, &jp);
    
      puts ("\nAfter swapping the pointers...\n");
      printf ("ip points to %p, which matches j (%p).  \nThe value at the end of the pointer is %d\n", (void*)ip, (void*)&j, *ip);
      printf ("\njp points to %p, which matches i (%p).  \nThe value at the end of the pointer is %d\n", (void*)jp, (void*)&i, *jp);
        
      return(0);
    }
    
    /*
    Output
    
    ip points to 0012FF80, which matches i (0012FF80).
    The value at the end of the pointer is 1
    
    jp points to 0012FF84, which matches j (0012FF84).
    The value at the end of the pointer is 2
    
    After swapping the pointers...
    
    ip points to 0012FF84, which matches j (0012FF84).
    The value at the end of the pointer is 2
    
    jp points to 0012FF80, which matches i (0012FF80).
    The value at the end of the pointer is 1
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    yeah i got that- thanks

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  12. #12
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Okay, sorry I'm a little late to this one, but of course C can pass by reference...

    foo(&variable);

    Whenever you pass the address of something, by definition you are passing it by reference (to it). That's the literal translation. But in C you have to do it explicitly (using the '&').

    ---

    But what annoys me is when people don't understand when the compiler is doing a little bit of extra preprocessing to figure out if something's being passed by address or not.

    In the given example, the C++ compiler sees the 'reference' notation (&) in the function declaration and remembers that each time that function is called. Then, when the function is called, it automagically adds the '&' to each field if it needs it, without the programmer being aware of it. A C compiler isn't so forgiving.

    Code:
    #include <stdio.h>
    
    int blah(int &a, int &b)
       {
       int c;
       c = a;
       a = b;
       b = c;
       return 0;
       }
    
    int main()
       {
       int a[2];
       a[0] = 34;
       a[1] = 32;
       printf("a = %d\nb = %d",a[0],a[1]);
       blah(a[0],a[1]);
       printf("a = %d\nb = %d",a[0],a[1]);
       return 0;
       }
    Essentially the compiler made up for a programmer's mistake without giving a warning or an error. The programmer then is never aware of their poorly written code.
    It is not the spoon that bends, it is you who bends around the spoon.

  13. #13
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    but of course C can pass by reference...
    No...C only supports passing by value.
    C cannot pass by reference. It only simulates it. It can pass a reference to something through a pointer variable.

    But a value is copied, and that value is an address, making it pass-by-value.

    P.S. If you don't believe me, I suggest you email this guy:
    dmr@bell-labs.com
    Last edited by Dante Shamest; 10-07-2003 at 02:09 PM.

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    161
    But a value is copied, and that value is an address, making it pass-by-value.
    C++ references work the exact same way - by passing an address on the stack. In fact, the following two programs are likely to generate the exact same code:

    Code:
    void func(int& i)
    {
      i = 10;
    }
    
    int main()
    {
      int i;
      func(i);
    }
    and...

    Code:
    void func(int* i)
    {
      *i = 10;
    }
    
    int main()
    {
      int i;
      func(&i);
    }

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Because C does not pass by reference, we have to do:

    Code:
    void attach(int ** ptr, int * address)
    {
     *ptr = address;
    }

    Instead of:

    Code:
    void attach(int * ptr, int * address)
    {
     ptr = address;
    }

    In C++ this is true as well, but now we can do:


    Code:
    void attach(int* & ptr, int * address)
    {
     ptr = address;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  3. Replies: 14
    Last Post: 03-18-2006, 08:14 AM
  4. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM
  5. passing by refrence problems
    By rippascal in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2002, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21