Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
Passing information into functions in C - by name, by value

If you pass a variable into a function in C, you copy the contents into the function so thst any changes made internally in the function are not reflected in the main code copy. However, if you pass in the address of the variable and work at the same address inside and outside the function, any changes you make to the variable's contents within the function will be reflected in the calling code too. Example [here].

From that example - defining the function that uses a local copy of the value

  void increase(int number) {
    number++;
    printf("Number is %d\n",number);
  }


and calling that:

  increase(people);

Alternative - defining the function that copies the address so that the variable is in effect aliased:

  void boost(int *number) {
    (*number)++;
    printf("Number is %d\n",*number);
  }


and calling that

  boost(&people);

Neither approach is necessarily right - they work in different circumstances. But do note that calling by address is the sensible and effieicent way to pass large data structures (or, rather, references to them) around.

If you want to learn C of C++, we run courses - see [here] or get in touch leting us know your background and what you'll be doing with C or C++ so we can best advise.
(written 2015-10-26, updated 2015-10-27)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C204 - C and C based languages - Functions, Macros and programs in multiple files
  [4557] Function prototype - what they are and why you should use them - C and C++ - (2015-10-27)
  [4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
  [4338] Passing arrays into functions in C - (2014-12-02)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [3717] Returning extra results from a function in C - (2012-05-03)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [2841] C Course exercise and sample answer - source in 2 files - (2010-06-30)
  [2575] Sharing variables between files of code in C - extern - (2010-01-14)
  [2570] Function Prototypes in C - (2010-01-11)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [775] Do not duplicate your code - (2006-06-23)


Back to
RUby - loading, using, changing, storing JSON format data
Previous and next
or
Horse's mouth home
Forward to
Preprocessor directives in C and C++ - what they mean
Some other Articles
Well House Consultants - Python courses / what's special.
Strings in C - strncmp strncpy and friends
Passing information into functions in C - by name, by value
RUby - loading, using, changing, storing JSON format data
Scenario outlines - tables of values to test - in Gherkin / Cucumber
Testing your new class - first steps with cucumber
Build up classes into applications sharing data types in Ruby
Clarrissa-Marybelle - too long to really fit?
4754 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4554_Pas ... value.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat