I would like to know what really are the differences between these two pieces of code and what are any advantages of one over the one, if any. Can there be undefined behavior with either one of these? I would assume not because there are no local variables in the Add function.
1st code:
#include <stdio.h>
#include <stdlib.h>
void Add(int a, int b, int *c)
{
*c = a + b;
}
int main()
{
int a = 2, b = 4, c;
Add(a, b, &c);
printf("Sum = %d\n", c);
return 0;
}
2nd code:
#include <stdio.h>
#include <stdlib.h>
int *Add(int a, int b, int *c)
{
*c = a + b;
return c;
}
int main()
{
int a = 2, b = 4, c, *ptr;
ptr = Add(a, b, &c);
printf("Sum = %d\n", *ptr);
return 0;
}