References in C?

This is a discussion on References in C? within the C Programming forums, part of the General Programming Boards category; I have heard that the reference thingy is a C++ thing, why isn't that in C? Well, when i send ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    References in C?

    I have heard that the reference thingy is a C++ thing, why isn't that in C? Well, when i send an array through a function, it becomes a reference, or the pointer to the first element in the array is sent, by how about references to ints? If I write a function
    Code:
    int lessthan(int int1, int int2) {
        return int1 < int2;
    }
    int1 and int2 is copies of some ither ints out there. But in C++ you can write
    Code:
    int lessthan(int &int1, int &int2) {
        return int1 < int2;
    }
    and then it's not, it becomes references and that is much faster. How do I achive the same performance in C as in C++?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Simulate pass-by-reference using a pointer.

    [edit]
    Code:
    int lessthan(int *int1, int *int2) {
        return *int1 < *int2;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Not really the point of this thread, but passing references to ints instead of ints is not faster. It's slower actually. The speed advantage of references (and pointers) is that the data isn't copied - but at the expense of the indirect access. If the data in question is as big as or even smaller than a reference, the advantage is lost and the disadvantage remains.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    So, maybe a reference in C++ is exactly the same as sending a pointer to the int and then use *theint instead? And I can see that that's slower, cause you will still have to copy the pointer.

    But how about inline functions? They will just take the arguments as expression and use the expressions is the function calling the inline function, right? And that has to be faster if the arguments is just single variables or values, right?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So, maybe a reference in C++ is exactly the same as sending a pointer to the int and then use *theint instead?
    http://parashift.com/c++-faq-lite/references.html
    But how about inline functions? They will just take the arguments as expression and use the expressions is the function calling the inline function, right? And that has to be faster if the arguments is just single variables or values, right?
    If you're just learning the language, work at correctness first.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    i guess this short sentence explains pretty well what a reference is:
    "a reference is a pointer that behaves syntactically like the object (it is pointing to) itself"
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler settings, references etc...
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2009, 04:53 AM
  2. VC++ 2005 Express: missing references page
    By psychopath in forum Tech Board
    Replies: 1
    Last Post: 08-21-2006, 04:55 PM
  3. Vector of references
    By roktsyntst in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 06:40 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 07:14 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

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