why does this code work?
#include <stdio.h>
void func(int v[]){
v[0] = 1;
}
int main(){
int v[5] = {0};
func(v);
for (int i = 0; i < 5; i++)
{
printf("%d ", v[i]);
}
}
The output I get from this is '1 0 0 0 0' but why? I'm not passing a pointer, why can the function change the array in my main?
Yes, you are passing a pointer.
When you write void func(int v[])
to declare your function signature, it is equivalent to writing void func(int * v)
.
When you write func(v)
to call your function, it is equivalent to func(&v[0])
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With