LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-19-2011, 03:19 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,225

Rep: Reputation: 49
Passing input arguments by reference in C.


Hi:
Is it common practice in C to write functions with an input argument such that the caller passes a pointer as the input argument? Example:
Code:
int john(struct bridge *arg1)
{
FUNCTION BODY
}

struct bridge _arg1;

int_1 = john(&_arg1);
 
Old 08-19-2011, 03:22 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by stf92 View Post
Hi:
Is it common practice in C to write functions with an input argument such that the caller passes a pointer as the input argument?
This is the most common way to pass struct arguments in C, often with the const qualifier if the argument isn't going to be changed. This is apparent when you start using Linux/Unix C functions.
Kevin Barry
 
1 members found this post helpful.
Old 08-19-2011, 03:42 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,225

Original Poster
Rep: Reputation: 49
Thanks a lot.
 
Old 08-19-2011, 07:15 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
The reason for that is because it's much more efficient to copy a pointer than a large struct.

Another use for it is for the function to be able to modify variables passed as parameters.
 
Old 08-19-2011, 09:24 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,225

Original Poster
Rep: Reputation: 49
Thanks a lot, MTK358.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Passing command line arguments jason_m Programming 4 08-26-2010 11:36 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 06:13 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 03:18 PM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 02:37 AM


All times are GMT -5. The time now is 02:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration