Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Argument passing=== In C, arguments are passed to functions [[pass by value|by value]] while other languages may pass variables [[pass by reference|by reference]]. This means that the receiving function gets copies of the values and has no direct way of altering the original variables. For a function to alter a variable passed from another function, the caller must pass its ''address'' (a ''pointer'' to it), which can then be dereferenced in the receiving function. See [[C syntax#Pointers|Pointers]] for more information. <syntaxhighlight lang=C> void incInt(int *y) { (*y)++; // Increase the value of 'x', in 'main' below, by one } int main(void) { int x = 0; incInt(&x); // pass a reference to the var 'x' return 0; } </syntaxhighlight> The function [[scanf]] works the same way: <syntaxhighlight lang=C> int x; scanf("%d", &x); </syntaxhighlight> In order to pass an editable pointer to a function (such as for the purpose of returning an allocated array to the calling code) you have to pass a pointer to ''that'' pointer: its address. <syntaxhighlight lang=C> #include <stdio.h> #include <stdlib.h> void allocate_array(int ** const a_p, const int A) { /* allocate array of A ints assigning to *a_p alters the 'a' in main() */ *a_p = malloc(sizeof(int) * A); } int main(void) { int * a; /* create a pointer to one or more ints, this will be the array */ /* pass the address of 'a' */ allocate_array(&a, 42); /* 'a' is now an array of length 42 and can be manipulated and freed here */ free(a); return 0; } </syntaxhighlight> The parameter {{code|int **a_p}} is a pointer to a pointer to an {{code|int}}, which is the address of the pointer {{code|p}} defined in the ''main'' function in this case. ====Array parameters==== Function parameters of array type may at first glance appear to be an exception to C's pass-by-value rule. The following program will print 2, not 1: <syntaxhighlight lang=C> #include <stdio.h> void setArray(int array[], int index, int value) { array[index] = value; } int main(void) { int a[1] = {1}; setArray(a, 0, 2); printf ("a[0]=%d\n", a[0]); return 0; } </syntaxhighlight> However, there is a different reason for this behavior. In fact, a function parameter declared with an array type is treated like one declared to be a pointer. That is, the preceding declaration of {{code|setArray}} is equivalent to the following: <syntaxhighlight lang=C> void setArray(int *array, int index, int value) </syntaxhighlight> At the same time, C rules for the use of arrays in expressions cause the value of {{code|a}} in the call to {{code|setArray}} to be converted to a pointer to the first element of array {{code|a}}. Thus, in fact this is still an example of pass-by-value, with the caveat that it is the address of the first element of the array being passed by value, not the contents of the array. Since C99, the programmer can specify that a function takes an array of a certain size by using the keyword {{code|static}}. In <code>void setArray(int array[static 4], int index, int value)</code> the first parameter must be a pointer to the first element of an array of length at least 4. It is also possible to add qualifiers (<code>const</code>, <code>volatile</code> and <code>restrict</code>) to the pointer type that the array is converted to by putting them between the brackets.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)