I am getting errors in the following code. The errors disappear if I take out "struct point p2...". p1 is assembled the same way and works fine, what is the catch here?
#include <stdio.h>
struct point {
int x;
int y;
};
struct point makepoint(int x, int y)
{
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
void main()
{
struct point p1 = makepoint(5, 7);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
struct point p2 = makepoint(2, 9);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
}
I suggest the following code, which compiles cleanly and exercises each function
#include <stdio.h>
struct point
{
int x;
int y;
};
void makepoint(int x, int y, struct point* newPoint)
{
newPoint->x = x;
newPoint->y = y;
} // end function: makepoint
void addpoint(struct point* p1, struct point* p2)
{
p1->x += p2->x;
p1->y += p2->y;
} // end function: addpoint
int main()
{
struct point p1;
struct point p2;
makepoint(5, 7, &p1);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
makepoint(2, 9, &p2);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
addpoint( &p1, &p2 );
printf("p1 = (%d, %d)\n", p1.x, p1.y);
return(0);
} // end function: main