0
& and * in C
I can't really understand when should i use & and * for a variable, i understand the concept but i cant use them when coding
3 Respostas
+ 1
& -> get the address of an variable
* -> declare a pointer / dereference a pointer
A pointer is an variable that stores an address.
Based on the definition above, the following statement is to declare a pointer initialized with an address:
int* ptr = &an_variable;
And this statement is to dereference the pointer:
printf("%d", *ptr);
which is equivalent to:
printf("%d", an_variable);
0
They are generally used when you need to allocate memory at run-time. See Standard Template Library and Data Structures, these are one of the implementations of pointers.