Because C and C++ pass all parameters and return values by value, not by reference.
So if you returned the struct you allocated, what the calling function would get is not the actual item, but a copy:
typedef struct MyStruct
{
int x;
int y;
} MS;
MS* pMS;
MS GetOne(int x, int y)
{
MS* p = (MS*) malloc(sizeof(MS));
p->x = x;
p->y = y;
pMS = p;
return *p;
}
int main()
{
MS p = GetOne(10, 20);
pMS->x++;
printf("%u,%u\n", p.x, p.y);
printf("%u,%u\n", pMS->x, pMS->y);
return 0;
}
gives you:
10,20
11,20