by nadavision · 4y ago · 0 comments
> "If you want to “return” memory from a function, you don’t have to use malloc/allocated storage; you can pass a pointer to a local data:
void getData(int *data) { data[0] = 1; data[1] = 4; data[2] = 9; }
void main() { int data[3]; getData(data); printf("%d\n", data[1]); } "
int a = 5;
int *x; // this is a pointer
x = &a;
int c = *x; // both c and *x are ints
If it were *data, it would be equivalent to *(data + 0), which is equivalent to data[0], which is an int. You don't want to pass an int, you want to pass an *int. int *x;
is declaring that the type of *x is int. int *x, y;
You're saying that both *x and y are integers. int* x, y.
Does somebody know?The name of an array decays to a pointer to the first element in various contexts. You could do `&data[0]` but it means exactly the same thing and would read as over-complicated things to C programmers.
*data would give you an int, &data would give you an int*.