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 03-23-2016, 03:23 PM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
What does & in a c function parameters mean


So if I have function int func( &a, *b );

What does &a mean

Thanks
 
Old 03-23-2016, 03:55 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
http://www.c-for-dummies.com/caio/po...cheatsheet.php
 
Old 03-24-2016, 05:41 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by knobby67 View Post
So if I have function int func( &a, *b );

What does &a mean

Thanks
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;
}
 
1 members found this post helpful.
Old 03-24-2016, 07:01 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you are looking at a C++ definition
 
Old 03-24-2016, 07:40 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(Edited)
Well, yes, the code-snippet in the original post is plain broken; if it was meant to be:
Code:
int func(type1 &a, type2 *b);
then the answer is: it is invalid in C, valid in C++

Last edited by NevemTeve; 03-24-2016 at 07:43 AM.
 
Old 03-24-2016, 01:42 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 03-24-2016, 11:01 PM   #7
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
It's hard to tell from that faulty code-snippet whether this is a function call or a function declaration.
it has a return type
 
Old 03-25-2016, 06:20 AM   #8
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
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.
 
Old 03-25-2016, 10:19 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by genss View Post
Quote:
Originally Posted by psionl0 View Post
It's hard to tell from that faulty code-snippet whether this is a function call or a function declaration.
it has a return type
But it lacks argument types.
 
Old 03-27-2016, 01:11 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The bottom line is that &a is C's style of pass-by-reference.
 
Old 03-28-2016, 01:36 AM   #11
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 03-28-2016, 02:05 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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').
 
Old 03-28-2016, 11:05 AM   #13
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
But it lacks argument types.
so it is clearly neither in C
 
Old 03-28-2016, 12:14 PM   #14
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
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 View Post
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.

Last edited by psionl0; 03-28-2016 at 12:25 PM.
 
Old 03-28-2016, 12:53 PM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by psionl0 View Post
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, 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.
 
  


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
[SOLVED] How to handle parameters in a .bashrc function whim Linux - Newbie 6 02-06-2015 08:10 AM
Function parameters in Lisp. zaxonus Programming 2 11-05-2011 08:27 AM
bash function parameters ArthurHuang Programming 1 05-18-2009 06:01 PM
Help me ... explain parameters of cacheflush() function minhstone Red Hat 2 01-22-2008 09:41 PM
C programming: Variable number of parameters to function kenneho Programming 8 03-21-2006 06:21 AM

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

All times are GMT -5. The time now is 07:56 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