Code:
#include <stdlib.h> // malloc, free
#include <stdio.h> // printf
#include <assert.h> // assert
#define SIZE(x) (sizeof(x)/sizeof(x[0]))
#define NEW(x) ((x)malloc(sizeof(struct x)))
typedef struct Point {
int x;
int y;
} *Point;
void Point_Update(Point const p, const int new_x, const new_y)
{
p->x = new_x;
p->y = new_y;
}
Point Point_New(const int init_x, const int init_y)
{
Point new = NEW(Point);
assert(new);
Point_Update(new, init_x, init_y);
return new;
}
void Point_Delete(const Point const point)
{
free(point);
}
void Point_Print(const Point const point)
{
printf("x = %d, y = %d\n", point->x, point->y);
}
Point Point_Search(const Point* arr, unsigned int siz, const int sum)
{
Point p;
while( siz-- ) {
p = *arr++;
if( p->x + p->y == sum ) { return p; }
}
return NULL;
}
int main(void)
{
Point points[3] = {
Point_New(1, 2),
Point_New(1, 99),
Point_New(100, 2)
};
Point found = Point_Search(points, SIZE(points), 100);
if( found ) { Point_Update(found, 0, 100); }
unsigned int i;
for( i = 0; i < SIZE(points); ++i ) {
Point_Print(points[i]);
Point_Delete(points[i]);
}
return 0;
}