Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Passing arrays into functions in C

When passing arrays into functions in C, you can specify just the array name and C will pass in a pointer to the first element of the array (i.e. &abc[0] is the same as saying just abc ). Within your functions, you can then use array style notation with square brackets, or pointers.

There's an example from the C course I'm running at the moment - [here] ... main code is

  main(){
  
    int stock[10];
    int k;
  
    for (k=0; k<10; k++) stock[k] = 100;
  
    int bobBuys[10];
    for (k=0; k<10; k++) bobBuys[k] = 0;
    bobBuys[4] = 50;
    bobBuys[6] = 10;
  
    updateStock(stock,bobBuys);
  
    showStock(stock);
  
}


You'll note how an apparently basic facility turns out to be rather elegant in how it calls up functions to ooerate on arrays as a whole, aided by the fact that passing an array is the same as passing the address of the first element.

Jelly Bean Picture? The example is all about stock levels and sales of Jelly beans

(written 2014-12-02)

 
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)
  [4554] Passing information into functions in C - by name, by value - (2015-10-26)
  [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)

C205 - C and C based languages - Arrays
  [4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3121] New year, new C Course - (2011-01-05)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [1614] When an array is not an array - (2008-04-17)


Back to
Learning to program sample program - past its prime, but still useful
Previous and next
or
Horse's mouth home
Forward to
Command line and file handling in C
Some other Articles
My first official measure - Passenger Entrys and Exits - revisited 9 years later
Segmentation Fault, Segmentation Violation, Bus Error, Stack Smashing
Command line and file handling in C
Simple C structs - building up to full, dynamic example
Passing arrays into functions in C
Learning to program sample program - past its prime, but still useful
Test Driven Development - a first example of principle in C
Flexible public courses - residential or commuting, programming newcomer or experienced, C or C++
Splitting out code into name blocks for clarity and reusability
Misty Melksham Morning
4736 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 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/4338_Pas ... -in-C.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat