LinuxQuestions.org
Review your favorite Linux distribution.
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 08-19-2011, 02:19 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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, 02: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, 02:42 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

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

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 08:24 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

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


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

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

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