#Parameters for functions
32 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
you are allowed
the way value parameters work is that x will copy what's put in the brackets to itself
for pointers this is different: you have to use temporary initialization (i think thats what its called) or take the address of another variable
;compile
#include <stdio.h>
int AddOne(int x) {
return x + 1;
}
int main() {
printf("%i\n", AddOne(5));
}```
6
for pointers this is different
Not really, no.
You could just as well do:
;compile
#include <stdio.h>
void foo(int *ptr) {
printf("%p\n", ptr);
}
int main() {
foo((int *) 0x40000000);
foo((int[]) { 1, 2, 3 });
}
0x40000000
0x7ffd4aa9a234
0x40000000
what's that even point to
is hi a value or macro
In this case: Nothing.
But especially on embedded systems or microchips you will often have some hardcoded memory addresses where certain stuff is, like e.g. a sensor reading or an LED port
e.g. I've got a device where setting a bit at address 0x00FF5E0218 will trigger a soft reset, so I've got this function:
void soft_reset(void) {
(uint8_t*)0x00FF5E0218 |= 0x10;
}
why 0x00FF5E0218
that's oddly specific
because that's what the manual says
RESET_CTRL (CRL_APB) Register Description Register Name RESET_CTRL Offset Address 0x0000000218 Absolute Address 0x00FF5E0218 (CRL_APB) Width 8 Type rwNormal read/write Reset Value 0x00000001 Description PS_SRST_B Pin Control and Trigger. RESET_CTRL (CRL_APB) Register Bit-Field Summary Field Name Bits Type Reset Value D...
are you at least allowed to assign a const value to remidn yourself what's there
of course
also what does (void) do
Or just a macro
it means no arguments
there's no parameter there so what's the point
yeh but you could just leave the brackets empty
that means unspecified arguments, not no arguments
When you don't put that you can actually call a function with as many arguments as you like (so long as there are no other parameters expected)
;compile
void foo() { };
void bar(void) { };
int main() {
foo(3, 2, 1);
bar(1, 2, 3);
}
<source>: In function 'main':
<source>:6:5: error: too many arguments to function 'bar'
6 | bar(1, 2, 3);
| ^~~
<source>:2:6: note: declared here
2 | void bar(void) { };
| ^~~
Build failed
Note how it doesn't complain about the foo