I've tried googling for an answer with no luck. My programming teacher just mentions it as a possibility that you can use, but I can't see the use cases, I think I understand how it works, so my theory is that it's faster to reference &var than using var. If that's the case is it just for that purpose? Is there something I'm missing? Am I completely wrong? EDIT: Sorry I meant - It's faster to reference a variable by using its address than just using the variable
To give other software references to variables:
double x0, y0;
double x1, y1;
double x2, y2;
/* Convert polar coordinates to rectangular coordinates.
This code needs two results from each call, but a function can only
return one thing. And we do not want the function to set the same
variables each time, so we cannot use fixed global variables. In
each call, we pass the function pointers to two variables we want
it to put the results in.
*/
ConvertPolarToRectangular(&x0, &y0, 3, .17);
ConvertPolarToRectangular(&x1, &y1, 4, .23);
ConvertPolarToRectangular(&x2, &y2, 5, -.03);
To work with data when we do not know how much we need when we compile the program:
// Get number of items needed.
scanf("%d", &N);
/* Allocate memory for records. This makes Records point to enough
memory for N records (if the malloc succeeds).
*/
struct Record *Records = malloc(N * sizeof *Records);
To create linked lists, trees, and other data structures that connect to each other:
struct ListItem
{
struct ListItem *Next;
int Data;
}
struct ListItem *FirstItem = malloc(sizeof *FirstItem);
struct ListItem *SecondItem = malloc(sizeof *SecondItem);
FirstItem->Next = SecondItem;
SecondItem->Next = NULL;
/* Now we can keep allocating memory for as many items as we need
and keep linking them together into a list. When we want to
delete one item, we can take it out of the chain without moving
the other items in memory:
*/
FirstItem->Next = ThirdItem;
free(SecondItem);
Besides performance, there is the ability to change the referenced variable.. If pass a variable to a function as an argument, the value of the variable is copied. If the function changes its argument, the original variable does not change. If you pass the address of a variable, however, then the function has the ability to change the original variable’s value. Sometimes that’s bad; sometimes that’s great. If you need that ability, then this is a crucial feature.
One common reason to take the address of a variable would be to allow a variable defined in one function to be changed in another.
In C, all parameters to functions are passed by value. So if you want a function to change the value of a variable defined in some other function, you need to pass its address.
For example:
void increment(int *x)
{
*x = *x + 1;
}
int main()
{
int num = 4;
increment(&num);
printf("num=%d\n", num);
}
Here, the address of num is passed to the increment function. This function can then dereference this pointer to read and write the value stored at that address. This code will output "num=5".