ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C does only "pass by value" (ie only a copy of the variable is passed to the function). Changes to the values passed to the function do not affect the original variables. If you wish a function to change the value of a variable then you need to pass the address of the variable to the function (that's what &a means) and use a pointer within the function.
The classic example is the swap function:
Code:
main() {
int a=5, b=3;
...
swap(&a, &b);
...
}
void swap(int *a, int *b) {
int t;
t = *a;
*a = *b;
*b = t;
}
It's hard to tell from that faulty code-snippet whether this is a function call or a function declaration.
In the latter case (assuming that this was C++) the &a would actually be a call by reference. ie changes to variable a in the function would change the original variable and no pointer would be needed.
Thanks for all the replies, sorry for some it's caused so much controversy.
I am an assembler/c programmer, low level driver writer, who has little ( no experience ) with c++ and c# ( I'm being asked to do more and more ). I had to link into a ( c++ ) library that has little or no documentation, so have had to work out functions from prototypes.
I accidental wrote c in thread header, I guessed what the function & did but thought I'd better check it up in case it caused issues in the future.
Any yes I did a search, but google wasn't my friend. I might have used the wrong wording, but thought I'd ask experienced people here, ( thanks to those who helped ) I'm one of those people who get's more info from a question than 50 pages in a book.
The bottom line is that &a is C++'s style of pass-by-reference.
ftfy.
More generally, it is a means of "aliasing" meaning that a variable can have 2 or more names.
So, int &a = i; means that a and i are the same variable.
Actually, it is an immutable pointer with special syntax. It is possible to have a reference to an object that has been deallocated or moved (known as 'dangling reference').
Actually, it is an immutable pointer with special syntax.
Admittedly it is a 1993 publication but the text that I use (Object-Oriented Programming Using C++ by Ira Pohl) doesn't mention "immutable pointer". It simply says that "Reference declarations declare the identifier to be an alternative name, or alias for an object specified in an initialization of the reference".
For an on-line link, http://www.cprogramming.com/tutorial/references.html says pretty much the same thing. It does note that "references are often implemented by the compiler writers as pointers" but as far as I know, the C++ standard doesn't mandate that references be set up that way.
Quote:
Originally Posted by NevemTeve
It is possible to have a reference to an object that has been deallocated or moved (known as 'dangling reference').
This is true. Also, making declarations such as int &y = *x or int& getLocalVariable() are just begging for trouble.
More generally, it is a means of "aliasing" meaning that a variable can have 2 or more names.
So, int &a = i; means that a and i are the same variable.
Actually, I think, "that may not actually be true."
Remember that "C" is an early(!) language, and that one might well say that "its stated purpose" was simply to be: "more expressive, and more system-independent, than Assembler."
It was, after all, originally conceived as a tool with which to write the Unix operating system!
If you think of "& ..." as being functionally-equivalent to ... "the address of ..." ... I think that you won't be too-far off. Beyond that, carefully check the manual.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.