Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: To directly answer your updated question: you have to pass in the size of the array. C has no mechanism to store the size of arrays like Java does. If the compiler knows about the size of the array because the array is a global or local variable, not dynamically allocated, then you can use the sizeof() operator. Otherwise, you have to know the size separately, or use sentinel values in your array (such as a 0.0 at the end, or a NULL).</p> <p>As for arrays, pointers and arguments in general, see below:</p> <p>You will be returning a pointer to the array, which is indicated with the '*' syntax:</p> <pre><code>int *returnArr(int[] a) { // modify a... return a; } int a[] = { ... }; int *b; b = returnArr(a); </code></pre> <p>A few things to note:</p> <ul> <li>You can't do assignments in variable declarations that involve non-constant expressions (e.g., function calls). This might have changed in C99, though.</li> <li><p>The brackets go <em>after</em> the variable name, unlike in Java where they are part of the type. Even though Java's syntax is more consistent, it doesn't quite make sense in C where you often give the array size in the brackets in the variable declaration:</p> <p><code>int a[3] = { ... };</code></p></li> <li><p>There's no way to specify that a function returns an array as opposed to a plain pointer. In C, array references decay to pointers (though pointers and arrays are NOT the same thing, as is commonly claimed). That means that whenever you pass an array around, C only provides a means to a pass a pointer to the array. The whole array isn't actually copied. As it happens, the name of the array is also a pointer to the first element of the array.</p></li> </ul> <p>Please also take note of what user268396 says in their answer. If you are planning to create a new array and return it, you'll need to either allocate the array dynamically, or have a pointer to an already allocated array be passed in (which is what it seems like you are kind of doing anyway).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload