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);