#include <stdio.h>
#include <stdlib.h>
typedefstruct {
int x, y;
} Point, * PointPtr; // or you could just use Point*
void init (PointPtr p) {p->x = 0; p->y = 0;}
void set (PointPtr p, int a, int b) {p->x = a; p->y = b;}
void showw (PointPtr p) {printf("%d %d\n", p->x, p->y);}
main(){
PointPtr p = (PointPtr) malloc(sizeof(Point)),
q = (PointPtr) malloc(sizeof(Point));
init(p);
set(q, 1, 2);
showw(p);
printf("%d %d\n", q->x, q->y);
free(p);
free(q);
}
That is, typedef the struct to Point and the pointer to PointPtr, so you can use sizeof(Point), rather than 8. Then if you add a new member to Point (e.g. color) you don't have to touch the code to change the size passed to malloc.
I know this isn't what they were asking for but I'm wondering; could they just swap out <stdio.h> & <stdlib.h> for <cstdio> & <cstdlib>?
[edit]: And 'using namespace std;' too.
If you're writing pure C, and have a .c extension, then you cannot use namespaces. Hence <cstdio>, etc are unusable (they just wrap <stdio.h>, etc in the std namespace anyway)
If you're compiling as C++, then it makes no odd if you use <stdio.h>, etc or <cstdio>, etc plus using namespace std.
I would prob. do the former if I'm just using C functions. I switch to <cstdio>, etc when I'm including things like <string>, <vector>, etc; to be consistent.