#include <stdio.h> #include <stdlib.h> /* declare the Point type */ typedef struct { int x, y; } Point; /* function to print a Point */ void printPoint( Point *p ) { printf("(%d, %d)\n", p->x, p->y ); } int main() { /* declare a Point and allocate memory */ Point *point = (Point *)malloc( sizeof( Point ) ); /* don't change these statements */ point->x = -23; point->y = 45; /* print values */ printPoint( point ); /* free memory */ free( point ); return 0; }
Notice how this works:
printPoint( point );
The pointer variable point
contains the address of the struct
.
It is this address that needs to be passed to the function.
Call-by-value does exactly that.
Sometimes students think that when a function parameter is an address that the function call must use an & ampersand:
printPoint( &point );
But this would pass the address of the variable point
, not the address of the struct
.