You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

587 lines
39 KiB

4 years ago
  1. <!doctype html>
  2. <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
  3. <!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
  4. <!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
  5. <!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
  6. <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Arduino FAQ - Passing arrays to functions</title>
  10. <base href="http://arduino.land/FAQ/" />
  11. <meta name="description" content="A big topic for beginners is how to write a function that can ...">
  12. <meta name="keywords" content=" function, reference,Arduino FAQ, pointer, arduino,cpp, c++, array, pass">
  13. <meta name="author" content="Christopher Andrews">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <meta name="application-name" content="phpMyFAQ 2.8.15">
  16. <meta name="robots" content="INDEX, FOLLOW">
  17. <meta name="revisit-after" content="7 days">
  18. <!-- Share on Facebook -->
  19. <meta property="og:title" content="Arduino FAQ - Passing arrays to functions" />
  20. <meta property="og:description" content="A big topic for beginners is how to write a function that can ..." />
  21. <meta property="og:image" content="" />
  22. <link rel="stylesheet" href="/../CSS/faq.main.scss">
  23. <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
  24. <link rel="shortcut icon" href="http://arduino.land/FAQ/assets/template/arduino-land-2/favicon.ico">
  25. <link rel="apple-touch-icon" href="http://arduino.land/FAQ/assets/template/arduino-land-2/apple-touch-icon.png">
  26. <link rel="canonical" href="http://arduino.land/FAQ/content/6/32/en/passing-arrays-to-functions.html">
  27. <script src="http://arduino.land/FAQ/assets/js/libs/modernizr.min.js"></script>
  28. <script src="http://arduino.land/FAQ/assets/js/phpmyfaq.min.js"></script>
  29. <link rel="alternate" title="News RSS Feed" type="application/rss+xml" href="http://arduino.land/FAQ/feed/news/rss.php">
  30. <link rel="alternate" title="TopTen RSS Feed" type="application/rss+xml" href="http://arduino.land/FAQ/feed/topten/rss.php">
  31. <link rel="alternate" title="Latest FAQ Records RSS Feed" type="application/rss+xml" href="http://arduino.land/FAQ/feed/latest/rss.php">
  32. <link rel="alternate" title="Open Questions RSS Feed" type="application/rss+xml" href="http://arduino.land/FAQ/feed/openquestions/rss.php">
  33. <link rel="search" type="application/opensearchdescription+xml" title="Arduino FAQ - Passing arrays to functions" href="http://arduino.land/FAQ/opensearch.html">
  34. </head>
  35. <body dir="ltr">
  36. <script>
  37. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  38. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  39. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  40. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  41. ga('create', 'UA-48223018-1', 'arduino.land');
  42. ga('require', 'linkid', 'linkid.js');
  43. ga('require', 'displayfeatures');
  44. ga('send', 'pageview');
  45. </script>
  46. <!--[if lt IE 8 ]>
  47. <div class="internet-explorer-error">
  48. Do you know that your Internet Explorer is out of date?<br/>
  49. Please use Internet Explorer 8+, Mozilla Firefox 4+, Google Chrome, Apple Safari 5+ or Opera 11+
  50. </div>
  51. <![endif]-->
  52. <div class="navbar navbar-fixed-top hidden-print">
  53. <div class="navbar-inner">
  54. <div class="container-fluid">
  55. <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  56. <span class="icon-bar"></span>
  57. <span class="icon-bar"></span>
  58. <span class="icon-bar"></span>
  59. </button>
  60. <a class="brand" title="Arduino FAQ" href="http://arduino.land/FAQ">Arduino FAQ</a>
  61. <nav class="nav-collapse">
  62. <ul class="nav">
  63. <li class=""><a href="http://arduino.land/FAQ/instantresponse.html">Instant Response</a></li>
  64. <li class=""><a href="http://arduino.land/FAQ/addcontent.html">Add new FAQ</a></li>
  65. <li class=""><a href="http://arduino.land/FAQ/ask.html">Add question</a></li>
  66. <li class=""><a href="http://arduino.land/FAQ/open.html">Open questions</a></li>
  67. </ul>
  68. <ul class="nav pull-right">
  69. <li class=""><a href="?action=register">Sign up</a></li>
  70. <li class="divider-vertical"></li>
  71. <li class=""><a href="?action=login">Login</a></li>
  72. </ul>
  73. </nav>
  74. </div>
  75. </div>
  76. </div>
  77. <section id="content" class="container-fluid">
  78. <div class="row-fluid">
  79. <div class="span3 hidden-print" id="leftContent">
  80. <div class="well categories">
  81. <ul class="nav nav-list">
  82. <li class="home"><a href="http://arduino.land/FAQ/index.html">FAQ Home</a></li>
  83. <li class="test-lst"><a href="http://arduino.land/FAQ/showcat.html">All categories</a></li>
  84. <li class="divider-horizontal"></li>
  85. <li><a id="category_1" title="This category holds commonly asked questions on the Arduino forum." href="/FAQ/category/1/using-arduino.html">Using Arduino <img src="assets/img/more.gif" width="11" height="11" alt="Using Arduino" style="border: none; vertical-align: middle;" /></a></li>
  86. <li><a id="category_0" href="/FAQ/showcat.html">Programming Arduino</a>
  87. <ul class="nav nav-list">
  88. <li><a id="category_4" title="A few closely related articles about memory, allocation, and general usage" href="/FAQ/category/4/memory-%26-storage.html">Memory & storage</a></li>
  89. <li class="active"><a class="active" id="category_6" title="A collection of answers specifically involving arrays." href="/FAQ/category/6/arrays.html">Arrays</a></li>
  90. <li><a id="category_8" title="A set of answers describing some common errors encountered in C++ and Arduino" href="/FAQ/category/8/errors.html">Errors</a></li>
  91. <li><a id="category_9" title="Topics centered around utilizing the AVR flash memory." href="/FAQ/category/9/progmem.html">PROGMEM</a></li>
  92. </ul>
  93. </li>
  94. <li><a id="category_3" title="This section contains FAQ articles that may be unrelated to Arduino & C++, but support FAQ&#39;s in other categories." href="/FAQ/category/3/miscellaneous.html">Miscellaneous</a></li>
  95. </ul>
  96. <div class="users-online">
  97. <small>1 user online | 1 Guest and 0 Registered</small>
  98. </div>
  99. </div>
  100. <div class="well adbox">
  101. <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  102. <ins class="adsbygoogle ad_sm_sq" data-ad-client="ca-pub-2585293717740875" data-ad-slot="4911891944"></ins>
  103. <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
  104. </div>
  105. </div>
  106. <div class="span6" id="mainContent">
  107. <div id="breadcrumbs" class="hidden-print">
  108. <ul class="breadcrumb"><li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/FAQ/category/2/programming-arduino.html" itemprop="url" rel="index"><span itemprop="title">Programming Arduino</span></a></li><li class="divider"> &raquo; </li><li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a title="A collection of answers specifically involving arrays." href="/FAQ/category/6/arrays.html" itemprop="url"><span itemprop="title">Arrays</span></a></li></ul>
  109. </div>
  110. <header>
  111. <div class="pull-right hidden-print" id="solution_id">
  112. <a class="label label-info" href="/FAQ/index.php?solution_id=1031">
  113. ID #1031
  114. </a>
  115. </div>
  116. <h2>Passing arrays to functions</h2>
  117. </header>
  118. <hr>
  119. <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  120. <!-- FAQ-banner-top -->
  121. <ins class="adsbygoogle"
  122. style="display:inline-block;width:468px;height:60px"
  123. data-ad-client="ca-pub-2585293717740875"
  124. data-ad-slot="6042234340"></ins>
  125. <script>
  126. (adsbygoogle = window.adsbygoogle || []).push({});
  127. </script>
  128. <hr>
  129. <article class="answer">
  130. <p>A big topic for beginners is how to write a function that can be passed an array. A very common way of achieving this is done using pointers. This method can be seen all through the C core in functions like <code>memcpy()</code>. Another way, which is the more natural choice for C++, but not as popular with beginners and C programmers is to pass the variable as an actual array. Both methods have their advantages, however each has their drawbacks. How you need to use the array in your own code should determine which is best.</p>
  131. <ul>
  132. <li><a href="/FAQ/content/6/32/en/passing-arrays-to-functions.html#pass-an-array-using-pointer"><strong>Pass an array using a pointer</strong></a>
  133. <ul>
  134. <li>
  135. <p><a title="Goto section." href="/FAQ/content/6/32/en/passing-arrays-to-functions.html#pass-multi-by-pointer"><strong>Pass a multidimensional array using pointer</strong></a></p>
  136. </li>
  137. </ul>
  138. </li>
  139. <li><strong><strong><a href="/FAQ/content/6/32/en/passing-arrays-to-functions.html#pass-an-array">Pass an array using array types</a> (single &amp; multidimensional)</strong></strong></li>
  140. </ul>
  141. <hr />
  142. <p> </p>
  143. <h4><a name="pass-an-array-using-pointer"></a>Pass an array using a pointer</h4>
  144. <p>An arrays name in code, without subscript operators <code>[]</code>, is implicitly convertible to a pointer of its first element. An example of this is shown below. The pointer to <code>arr[0]</code> is assigned to the variable <code>ptr</code>. Contrary to popular belief, this is not a pointer to the array, and the array has implicitly been cast to a pointer, which is commonly referred to as <em>decayed pointer</em>, as it has been stripped of the information an array type provides. The example also shows how you can explicitly get the pointer of any array element.</p>
  145. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">char arr[] = { 1, 2, 3, 4, 5 };
  146. char *ptr = arr;
  147. //Or the equivalent ( change index to start mid-array ):
  148. char *ptr2 = &amp;arr[0];</pre>
  149. <p>Once we have a pointer to an element, you can also use pointer arithmetic to navigate the array. When passing an array to a function using pointers, you can convert it to a pointer using the two methods explained above. The next example shows this basic principle in the function <code>setup()</code>:</p>
  150. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">#define MAX_LEN 5
  151. char arr[ MAX_LEN ] = { 1, 2, 3, 4, 5 };
  152. void Func( char *ptr ){
  153. for( int idx = MAX_LEN - 1 ; idx &gt;= 0 ; --idx ){
  154. Serial.println( ptr[ idx ], HEX );
  155. }
  156. }
  157. void setup(){
  158. Serial.begin( 9600 );
  159. Func( arr );
  160. }
  161. void loop() {}</pre>
  162. <p>The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more generic and increase its reuseability, we can remove the hard coded length and pass it through the function also.</p>
  163. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">char arr[] = { 1, 2, 3, 4, 5 };
  164. void Func( char *ptr, int length ){
  165. while( length ){
  166. Serial.println( ptr[ --length ], HEX );
  167. }
  168. }
  169. void setup(){
  170. Serial.begin( 9600 );
  171. Func( arr, 5 );
  172. Serial.println( "----------------" );
  173. Func( &amp;arr[2], 3 );
  174. }
  175. void loop() {}</pre>
  176. <p>Now the function allows arrays of any length you want. This method is almost always required to deal with an array of unknown size as the typical methods of <a title="Goto FAQ article" href="content/6/29/en/how-to-get-the-size-of-an-array.html" target="_blank">retrieving the length of an array</a> do not work with the pointer.</p>
  177. <h5><a name="pass-multi-by-pointer"></a>Pass a multidimensional array using pointer</h5>
  178. <p>The above example is a common and accepted way of dealing with array data. However this method is not as straight forward when using multidimensional arrays, for example, two dimensional array types do not convert to a 2D pointer (something like <code>char **ptr</code>). To pass a multidimensional array you must know the value of all dimensions except the first, so things become a little more restrictive.</p>
  179. <p>This short example shows how a multidimensional array can be passed. The functions <code>Func()</code> &amp; <code>Func2()</code> are two different styles used to pass a multidimensional array, and just like the examples above you can add in extra parameters to tell the function the length of each dimension:</p>
  180. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">void Func( char array[][3] ){
  181. return;
  182. }
  183. void Func2( char (*array)[3] ){
  184. return;
  185. }
  186. void setup() {
  187. char arr[][3] = {
  188. { 0, 1, 2 },
  189. { 3, 4, 5 },
  190. { 6, 7, 8 }
  191. };
  192. Func( arr );
  193. Func2( arr );
  194. }
  195. void loop() {}</pre>
  196. <p>As you can see, with each extra dimension, the functions accepting the pointer become less and less generic and more specific to a particular array. This is where templates could be used to gain a more generic interface<span>. However templates can provide many more capabilities and these are explained in the next section below.</span></p>
  197. <hr />
  198. <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  199. <!-- FAQ-banner-top -->
  200. <ins class="adsbygoogle"
  201. style="display:inline-block;width:468px;height:60px"
  202. data-ad-client="ca-pub-2585293717740875"
  203. data-ad-slot="6042234340"></ins>
  204. <script>
  205. (adsbygoogle = window.adsbygoogle || []).push({});
  206. </script>
  207. <hr/>
  208. <h4><a name="pass-an-array"></a>Pass an array using array types</h4>
  209. <p>Rather than having an array decay to a pointer every time you need to use it, you can utilize a reference object. Arrays themselves are types, which means we can create pointers and references to them just like any other type. The syntax becomes a little obscure due to the nature of the array declaration, however it allows for a different style of programming to the pointer methods in the section above.</p>
  210. <p>For example, we cannot simply add an <code>&amp;</code> symbol to mark the array as a reference like: <code>char &amp;array[5]</code> as this declares an array of references, which is illegal, and not what we want (a reference to an array). The <code>&amp;</code> needs to be inside brackets to show its part of the array variable, and not the array data type: <code>char (&amp;array)[5]</code>.</p>
  211. <p>In the following example, a function taking a reference to an array can replace the first two examples in the pointer method above:</p>
  212. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">void Func( char (&amp;array)[5] ){
  213. for( int idx = 0 ; idx &lt; sizeof( array ) ; ++idx ){
  214. Serial.println( array[ idx ], HEX );
  215. }
  216. }
  217. void setup() {
  218. Serial.begin( 9600 );
  219. char arr[] = { 1,2,3,4,5 };
  220. Func( arr );
  221. }
  222. void loop(){}</pre>
  223. <p> If you haven&#39;t already noticed, the function does not need the length to be passed in as <code>sizeof</code> will work correctly with a reference. The same function can be rewritten using an actual <em>pointer to array</em>, the only difference is the need to <em>dereference</em> the pointer back to an array before using it:</p>
  224. <pre class="brush: bash;collapse: true;fontsize: 100; first-line: 1; ">void Func( char (*array)[5] ){
  225. for( int idx = 0 ; idx &lt; sizeof( *array ) ; ++idx ){
  226. Serial.println( (*array)[ idx ], HEX );
  227. }
  228. }
  229. //...
  230. char arr[] = { 1,2,3,4,5 };
  231. Func( &amp;arr ); //Pass the &#39;address of&#39; the array.</pre>
  232. <p>These examples, while useful, are still bound to an array of a certain length and are not quite as flexible as one may need. To write a function which will accept any size array, we can start to adopt some template features. The first change we can make is to let the compiler deduce the length of the array, so we can write one piece of code for any sized array (single dimension).</p>
  233. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">template&lt; size_t N &gt;
  234. void Func( char (&amp;array)[N] ){
  235. for( int idx = 0 ; idx &lt; N ; ++idx ){
  236. Serial.println( array[ idx ], HEX );
  237. }
  238. }</pre>
  239. <p>The template parameter <code>N</code> will be replaced with a usable value when called with an array. And in the declaration we can replace the hard coded value with <code>N</code>. As this value is accessible as a constant within the function, it also eliminates the need for sizeof.</p>
  240. <p>The next modification we can add is to allow arrays of any data type to use the function. This requires an additional template parameter, which we can use to replace the array type <code>char</code>:</p>
  241. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">template&lt; typename T, size_t N &gt;
  242. void Func( T (&amp;array)[N] ){
  243. //code here
  244. }</pre>
  245. <p>To write a version of this function for multidimensional arrays we can simply add a new template parameter for each dimension:</p>
  246. <pre class="brush: cpp;collapse: true; fontsize: 100; first-line: 1; ">template&lt; typename T, size_t N, size_t X &gt;
  247. void Func( T (&amp;array)[N][X] ){
  248. //Code here
  249. }<br />
  250. template&lt; typename T, size_t N, size_t X, size_t Y &gt;
  251. void Func( T (&amp;array)[N][X][Y] ){
  252. //Code here
  253. }</pre>
  254. <p>The benefit is each dimension is known and can be easily iterated. Heavy use of template style functions with different sized arrays could lead to code bloat, so these functions could be used as wrappers for pointer style functions, or to map multidimensional arrays onto functions accepting single dimension, which can be optimized very well by the compiler.</p>
  255. Tags: <a title="array" href="/FAQ/tags/35/array.html">array</a>, <a title="function" href="/FAQ/tags/130/function.html">function</a>, <a title="pointer" href="/FAQ/tags/131/pointer.html">pointer</a>, <a title="reference" href="/FAQ/tags/2/reference.html">reference</a>
  256. <aside id="faqAccordion" class="accordion related-faqs hidden-print">
  257. <div class="accordion-group">
  258. <div class="accordion-heading">
  259. <a class="accordion-toggle" href="#collapseRelatedFaqs" data-parent="#faqAccordion" data-toggle="collapse">
  260. Related entries
  261. </a>
  262. </div>
  263. <div id="collapseRelatedFaqs" class="accordion-body collapse">
  264. <div class="accordion-inner"><ul><li><a title="What is the Arduino language" href="/FAQ/content/1/1/en/what-is-the-arduino-language.html">What is the Arduino language</a></li><li><a title="Can I use int main() with Arduino" href="/FAQ/content/2/2/en/can-i-use-int-main-with-arduino.html">Can I use int main() with Arduino</a></li><li><a title="What does the IDE change in my sketch" href="/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html">What does the IDE change in my sketch</a></li><li><a title="I can&#39;t upload a sketch with !!! in a string" href="/FAQ/content/1/6/en/i-can_t-upload-a-sketch-with-in-a-string.html">I can&#39;t upload a sketch with !!! in a string</a></li><li><a title="What is the maximum number of elements an array can have" href="/FAQ/content/6/7/en/what-is-the-maximum-number-of-elements-an-array-can-have.html">What is the maximum number of elements an array can have</a></li></ul></div>
  265. </div>
  266. </div>
  267. </aside>
  268. </article>
  269. <script>
  270. $(function(){
  271. $('abbr[rel="tooltip"]').tooltip();
  272. });
  273. </script>
  274. <ul id="tab" class="nav nav-tabs hidden-print">
  275. <li class="active"><a href="#authorInfo" data-toggle="tab">About this FAQ</a></li>
  276. <li><a href="#votingForm" data-toggle="tab">Rate this FAQ</a></li>
  277. <li></li>
  278. </ul>
  279. <div class="tab-content faq-information">
  280. <div class="tab-pane active" id="authorInfo">
  281. <dl class="dl-horizontal">
  282. <dt>Last update: </dt><dd>2014-10-26 06:02</dd>
  283. <dt>Author:</dt><dd>Christopher Andrews</dd>
  284. <dt>Revision:</dt><dd>1.0</dd>
  285. </dl>
  286. </div>
  287. <div class="tab-pane hidden-print" id="votingForm">
  288. <form action="#" method="post" class="form-inline" accept-charset="utf-8">
  289. <input type="hidden" name="artikel" value="32">
  290. <div id="votings"></div>
  291. <div class="star-rating">
  292. <span data-stars="5"></span>
  293. <span data-stars="4"></span>
  294. <span data-stars="3"></span>
  295. <span data-stars="2"></span>
  296. <span data-stars="1"></span>
  297. </div>
  298. <div class="pull-right">
  299. <strong>Average rating:</strong><span id="rating"> 4.5 (8 Votes)</span>
  300. </div>
  301. </form>
  302. </div>
  303. <div class="tab-pane hidden-print" id="switchAvailableLanguage">
  304. </div>
  305. <div class="tab-pane hidden-print" id="addTranslation">
  306. <form action="/FAQ/index.php?action=translate&amp;cat=6&amp;id=32&amp;srclang=en" method="post" class="form-inline" accept-charset="utf-8">
  307. <select class="language" name="translation" id="translation" size="1">
  308. <option value="ar">Arabic</option>
  309. <option value="eu">Basque</option>
  310. <option value="bn">Bengali</option>
  311. <option value="bs">Bosnian</option>
  312. <option value="pt-br">Brazilian Portuguese</option>
  313. <option value="zh">Chinese (Simplified)</option>
  314. <option value="tw">Chinese (Traditional)</option>
  315. <option value="cs">Czech</option>
  316. <option value="da">Danish</option>
  317. <option value="nl">Dutch</option>
  318. <option value="fi">Finnish</option>
  319. <option value="fr">French</option>
  320. <option value="fr-ca">French (Canada)</option>
  321. <option value="de">German</option>
  322. <option value="el">Greek</option>
  323. <option value="he">Hebrew</option>
  324. <option value="hi">Hindi</option>
  325. <option value="hu">Hungarian</option>
  326. <option value="id">Indonesian</option>
  327. <option value="it">Italian</option>
  328. <option value="ja">Japanese</option>
  329. <option value="ko">Korean</option>
  330. <option value="lv">Latvian</option>
  331. <option value="lt">Lithuanian</option>
  332. <option value="ms">Malay</option>
  333. <option value="nb">Norwegian Bokm&aring;l</option>
  334. <option value="fa">Persian</option>
  335. <option value="pl">Polish</option>
  336. <option value="pt">Portuguese</option>
  337. <option value="ro">Romanian</option>
  338. <option value="ru">Russian</option>
  339. <option value="sr">Serbian</option>
  340. <option value="sk">Slovak</option>
  341. <option value="sl">Slovenian</option>
  342. <option value="es">Spanish</option>
  343. <option value="sv">Swedish</option>
  344. <option value="th">Thai</option>
  345. <option value="tr">Turkish</option>
  346. <option value="uk">Ukrainian</option>
  347. <option value="vi">Vietnamese</option>
  348. <option value="cy">Welsh</option>
  349. </select>
  350. <button class="btn btn-primary" type="submit" name="submit">
  351. Start translation...
  352. </button>
  353. </form>
  354. </div>
  355. </div>
  356. <p class="hidden-print">You can <a href="javascript:void(0);" onclick="javascript:$('#commentForm').show();">comment this FAQ</a></p>
  357. <a id="comment"></a>
  358. <div id="commentForm" class="hide">
  359. <form id="formValues" action="#" method="post" class="form-horizontal" accept-charset="utf-8">
  360. <input type="hidden" name="id" id="id" value="32">
  361. <input type="hidden" name="lang" id="lang" value="en">
  362. <input type="hidden" name="type" id="type" value="faq">
  363. <div class="control-group">
  364. <label class="control-label" for="user">Your name:</label>
  365. <div class="controls">
  366. <input type="text" id="user" name="user" value="" required>
  367. </div>
  368. </div>
  369. <div class="control-group">
  370. <label class="control-label" for="mail">Email</label>
  371. <div class="controls">
  372. <input type="email" id="mail" name="mail" value="" required>
  373. </div>
  374. </div>
  375. <div class="control-group">
  376. <label class="control-label" for="comment_text">Your comment:</label>
  377. <div class="controls">
  378. <textarea id="comment_text" name="comment_text" required></textarea>
  379. </div>
  380. </div>
  381. <div class="control-group"> <label class="control-label"></label> <div class="controls"><img id="captchaImage" src="/FAQ/index.php?action=writecomment&amp;gen=img&amp;ck=1549085170" height="40" width="165" alt="Chuck Norris has counted to infinity. Twice." title="click to refresh"> </div></div><div class="control-group"><label class="control-label">Please enter the captcha code</label> <div class="controls"> <div class="input-append"><input type="text" name="captcha" id="captcha" size="6" required><a class="btn" id="captcha-button" data-action="writecomment"><i class="icon-refresh"></i></a> </div> </div></div>
  382. <div class="form-actions">
  383. <button class="btn btn-primary" id="submitcomment" type="submit">
  384. submit
  385. </button>
  386. </div>
  387. </form>
  388. </div>
  389. <div id="loader"></div>
  390. <div id="comments">
  391. <p class="comment"><img class="commment-bubbles" src="assets/img/bubbles.gif" /><strong>Comment of <a href="mailto:no_AT_email_DOT_com">per</a>:</strong><br />Thanks so much for this FAQ it was very useful to me. A small typo, in <span class="comment-dots-32">... </span><a onclick="showLongComment(32)" class="comment-show-more-32 pointer">show more</a><span class="comment-more-32 hide">the last code sample there is a &#60;br&#62; that I don&#39;t think was intended. </span><br /><small>Added at: 2015-07-08 13:56</small></p>
  392. </div>
  393. <script>
  394. $(function() {
  395. $("div.star-rating > span").on("click", function(e) {
  396. var numStars = $(e.target).data("stars");
  397. saveVoting("faq", 32, numStars, "en");
  398. });
  399. });
  400. $('form#formValues').on('submit', function (e) {
  401. e.preventDefault();
  402. saveFormValues('savecomment', 'comment');
  403. return false;
  404. });
  405. </script>
  406. <style scoped>
  407. @import "http://arduino.land/FAQ/assets/js/syntaxhighlighter/styles/shCore.css";
  408. @import "http://arduino.land/FAQ/assets/js/syntaxhighlighter/styles/shThemeDefault.css";
  409. </style>
  410. <script src="http://arduino.land/FAQ/assets/js/syntaxhighlighter/scripts/shCore.js"></script>
  411. <script src="http://arduino.land/FAQ/assets/js/syntaxhighlighter/scripts/shAutoloader.js"></script>
  412. <script>
  413. SyntaxHighlighter.autoloader(
  414. 'js jscript javascript assets/js/syntaxhighlighter/scripts/shBrushJScript.js',
  415. 'applescript assets/js/syntaxhighlighter/scripts/shBrushAppleScript.js',
  416. 'xml xhtml xslt html assets/js/syntaxhighlighter/scripts/shBrushXml.js',
  417. 'bash shell assets/js/syntaxhighlighter/scripts/shBrushBash.js',
  418. 'php assets/js/syntaxhighlighter/scripts/shBrushPhp.js',
  419. 'sql assets/js/syntaxhighlighter/scripts/shBrushSql.js',
  420. 'cpp assets/js/syntaxhighlighter/scripts/shBrushCpp.js',
  421. 'plain assets/js/syntaxhighlighter/scripts/shBrushPlain.js'
  422. );
  423. //This is a temporary fix and can be removed once a proper solution is found.
  424. //Removes unrendered glossary entries injected into source code.
  425. $( 'pre' ).find( 'abbr,mark' ).replaceWith( function (){ return $( this ).text(); } );
  426. SyntaxHighlighter.all();
  427. </script>
  428. </div>
  429. <div class="span3 hidden-print" id="rightContent">
  430. <section class="well">
  431. <div id="social">
  432. <a href="https://www.facebook.com/sharer.php?u=http%3A%2F%2Farduino.land%2FFAQindex.php%3Faction%3Dartikel%26cat%3D6%26id%3D32%26artlang%3Den" target="_blank" rel="nofollow">
  433. <img src="http://arduino.land/FAQ/assets/img/facebook.png" alt="Share on Facebook" title="Share on Facebook" width="32" height="32" >
  434. </a>
  435. <a href="https://twitter.com/share?url=http%3A%2F%2Farduino.land%2FFAQindex.php%3Faction%3Dartikel%26cat%3D6%26id%3D32%26artlang%3Den&text=Passing+arrays+to+functions+%7C+http%3A%2F%2Farduino.land%2FFAQindex.php%3Faction%3Dartikel%26cat%3D6%26id%3D32%26artlang%3Den" target="_blank" rel="nofollow">
  436. <img src="http://arduino.land/FAQ/assets/img/twitter.png" alt="Share on Twitter" title="Share on Twitter" width="32" height="32" >
  437. </a>
  438. <a href="http://digg.com/submit?phase=2&url=http%3A%2F%2Farduino.land%2FFAQindex.php%3Faction%3Dartikel%26cat%3D6%26id%3D32%26artlang%3Den%26title%3DPassing%2Barrays%2Bto%2Bfunctions" target="_blank" rel="nofollow">
  439. <img src="http://arduino.land/FAQ/assets/img/digg.png" alt="Digg it!" title="Digg it!" width="32" height="32" >
  440. </a>
  441. <a href="http://arduino.land/FAQ?action=send2friend&cat=6&id=32&artlang=en" rel="nofollow">
  442. <img src="http://arduino.land/FAQ/assets/img/email.png" alt="Send FAQ to a friend" title="Send FAQ to a friend" width="32" height="32" >
  443. </a>
  444. <a target="_blank" href="/FAQ/pdf.php?cat=6&id=32&artlang=en" rel="nofollow">
  445. <img src="http://arduino.land/FAQ/assets/img/pdf.png" alt="PDF version" title="PDF version" width="32" height="32" >
  446. </a>
  447. <a href="javascript:window.print();" rel="nofollow">
  448. <img src="http://arduino.land/FAQ/assets/img/print.png" alt="Print this record" title="Print this record" width="32" height="32" >
  449. </a>
  450. </div>
  451. <div id="facebookLikeButton">
  452. </div>
  453. </section>
  454. <section class="well">
  455. <header>
  456. <h3>Records in this category</h3>
  457. </header>
  458. <div id="allCategoryArticles-content">
  459. <ul class="phpmyfaq_ul"><li><a title="What is the maximum number of elements an array can have" href="/FAQ/content/6/7/en/what-is-the-maximum-number-of-elements-an-array-can-have.html">What is the maximum number of elements an array can have</a></li><li><a title="How to get the size of an array" href="/FAQ/content/6/29/en/how-to-get-the-size-of-an-array.html">How to get the size of an array</a></li><li><a title="How to copy an array" href="/FAQ/content/6/30/en/how-to-copy-an-array.html">How to copy an array</a></li><li><a title="Passing arrays to functions" href="/FAQ/content/6/32/en/passing-arrays-to-functions.html">Passing arrays to functions</a></li></ul>
  460. </div>
  461. </section>
  462. <section class="well">
  463. <header>
  464. <h3>Tags</h3>
  465. </header>
  466. <div id="tagcloud-content">
  467. <div id="tagcloud-content"><span class="relevance4"><a title="Arduino (4)" href="/FAQ/tags/1/arduino.html">Arduino</a> </span><span class="relevance5"><a title="array (5)" href="/FAQ/tags/35/array.html">array</a> </span><span class="relevance1"><a title="assignment (1)" href="/FAQ/tags/83/assignment.html">assignment</a> </span><span class="relevance1"><a title="C (1)" href="/FAQ/tags/95/c.html">C</a> </span><span class="relevance2"><a title="C++ (2)" href="/FAQ/tags/36/c.html">C++</a> </span><span class="relevance1"><a title="C++11 (1)" href="/FAQ/tags/88/c11.html">C++11</a> </span><span class="relevance1"><a title="class (1)" href="/FAQ/tags/3/class.html">class</a> </span><span class="relevance1"><a title="compile (1)" href="/FAQ/tags/94/compile.html">compile</a> </span><span class="relevance1"><a title="copy (1)" href="/FAQ/tags/96/copy.html">copy</a> </span><span class="relevance2"><a title="delete (2)" href="/FAQ/tags/18/delete.html">delete</a> </span><span class="relevance4"><a title="error (4)" href="/FAQ/tags/40/error.html">error</a> </span><span class="relevance1"><a title="extensions (1)" href="/FAQ/tags/48/extensions.html">extensions</a> </span><span class="relevance1"><a title="external (1)" href="/FAQ/tags/45/external.html">external</a> </span><span class="relevance1"><a title="fail (1)" href="/FAQ/tags/92/fail.html">fail</a> </span><span class="relevance1"><a title="for (1)" href="/FAQ/tags/39/for.html">for</a> </span><span class="relevance2"><a title="free (2)" href="/FAQ/tags/20/free.html">free</a> </span><span class="relevance1"><a title="function (1)" href="/FAQ/tags/130/function.html">function</a> </span><span class="relevance2"><a title="IDE (2)" href="/FAQ/tags/89/ide.html">IDE</a> </span><span class="relevance1"><a title="ifdef (1)" href="/FAQ/tags/41/ifdef.html">ifdef</a> </span><span class="relevance1"><a title="ino (1)" href="/FAQ/tags/47/ino.html">ino</a> </span><span class="relevance1"><a title="invalid (1)" href="/FAQ/tags/82/invalid.html">invalid</a> </span><span class="relevance1"><a title="libraries (1)" href="/FAQ/tags/23/libraries.html">libraries</a> </span><span class="relevance1"><a title="library (1)" href="/FAQ/tags/93/library.html">library</a> </span><span class="relevance1"><a title="loop (1)" href="/FAQ/tags/38/loop.html">loop</a> </span><span class="relevance1"><a title="main (1)" href="/FAQ/tags/42/main.html">main</a> </span><span class="relevance2"><a title="malloc (2)" href="/FAQ/tags/19/malloc.html">malloc</a> </span><span class="relevance1"><a title="modules (1)" href="/FAQ/tags/71/modules.html">modules</a> </span><span class="relevance1"><a title="monitor (1)" href="/FAQ/tags/44/monitor.html">monitor</a> </span><span class="relevance1"><a title="multiple (1)" href="/FAQ/tags/72/multiple.html">multiple</a> </span><span class="relevance2"><a title="new (2)" href="/FAQ/tags/17/new.html">new</a> </span><span class="relevance1"><a title="pointer (1)" href="/FAQ/tags/131/pointer.html">pointer</a> </span><span class="relevance2"><a title="reference (2)" href="/FAQ/tags/2/reference.html">reference</a> </span><span class="relevance1"><a title="serial (1)" href="/FAQ/tags/43/serial.html">serial</a> </span><span class="relevance1"><a title="sketch (1)" href="/FAQ/tags/81/sketch.html">sketch</a> </span><span class="relevance1"><a title="stop (1)" href="/FAQ/tags/80/stop.html">stop</a> </span><span class="relevance1"><a title="struct (1)" href="/FAQ/tags/98/struct.html">struct</a> </span><span class="relevance1"><a title="tabs (1)" href="/FAQ/tags/73/tabs.html">tabs</a> </span><span class="relevance1"><a title="ternary (1)" href="/FAQ/tags/24/ternary.html">ternary</a> </span><span class="relevance1"><a title="typedef (1)" href="/FAQ/tags/97/typedef.html">typedef</a> </span><span class="relevance2"><a title="undefined (2)" href="/FAQ/tags/4/undefined.html">undefined</a> </span><span class="relevance1"><a title="upload (1)" href="/FAQ/tags/91/upload.html">upload</a></span></div>
  468. </div>
  469. </section>
  470. </div>
  471. </div>
  472. </section>
  473. <footer id="footer" class="container-fluid hidden-print">
  474. <div class="row-fluid">
  475. <div class="span6">
  476. <ul class="footer-menu">
  477. <li><a href="http://arduino.land/FAQ/sitemap/A/en.html">Sitemap</a></li>
  478. <li><a href="http://arduino.land/FAQ/contact.html">Contact</a></li>
  479. <li><a href="http://arduino.land/FAQ/glossary.html">FAQ Glossary</a></li>
  480. <li><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  481. <!-- Arduino_Main -->
  482. <ins class="adsbygoogle ad_reg_rect"
  483. data-ad-client="ca-pub-2585293717740875"
  484. data-ad-slot="3848155541"></ins>
  485. <script>
  486. (adsbygoogle = window.adsbygoogle || []).push({});
  487. </script></li>
  488. </ul>
  489. </div>
  490. <div class="span6">
  491. <form action="?sid=557822" method="post" class="pull-right" accept-charset="utf-8">
  492. <select class="language" name="language" id="language" size="1" onchange="this.form.submit();">
  493. <option value="ar">Arabic</option>
  494. <option value="eu">Basque</option>
  495. <option value="bn">Bengali</option>
  496. <option value="bs">Bosnian</option>
  497. <option value="pt-br">Brazilian Portuguese</option>
  498. <option value="zh">Chinese (Simplified)</option>
  499. <option value="tw">Chinese (Traditional)</option>
  500. <option value="cs">Czech</option>
  501. <option value="da">Danish</option>
  502. <option value="nl">Dutch</option>
  503. <option value="en" selected="selected">English</option>
  504. <option value="fi">Finnish</option>
  505. <option value="fr">French</option>
  506. <option value="fr-ca">French (Canada)</option>
  507. <option value="de">German</option>
  508. <option value="el">Greek</option>
  509. <option value="he">Hebrew</option>
  510. <option value="hi">Hindi</option>
  511. <option value="hu">Hungarian</option>
  512. <option value="id">Indonesian</option>
  513. <option value="it">Italian</option>
  514. <option value="ja">Japanese</option>
  515. <option value="ko">Korean</option>
  516. <option value="lv">Latvian</option>
  517. <option value="lt">Lithuanian</option>
  518. <option value="ms">Malay</option>
  519. <option value="nb">Norwegian Bokm&aring;l</option>
  520. <option value="fa">Persian</option>
  521. <option value="pl">Polish</option>
  522. <option value="pt">Portuguese</option>
  523. <option value="ro">Romanian</option>
  524. <option value="ru">Russian</option>
  525. <option value="sr">Serbian</option>
  526. <option value="sk">Slovak</option>
  527. <option value="sl">Slovenian</option>
  528. <option value="es">Spanish</option>
  529. <option value="sv">Swedish</option>
  530. <option value="th">Thai</option>
  531. <option value="tr">Turkish</option>
  532. <option value="uk">Ukrainian</option>
  533. <option value="vi">Vietnamese</option>
  534. <option value="cy">Welsh</option>
  535. </select>
  536. <input type="hidden" name="action" value="" />
  537. </form>
  538. </div>
  539. </div>
  540. <div class="row">
  541. <p class="copyright pull-right">
  542. powered by <a href="http://www.phpmyfaq.de" target="_blank">phpMyFAQ</a> 2.8.15
  543. </p>
  544. </div>
  545. </footer>
  546. </body>
  547. </html>