amit_iit
November 24th, 2004, 11:42 PM
Consider the following code
typedef union
{
struct /* Use this for the first array element*/
{
char title[20];
int count;
} header;
struct /* Use this for remaining array elements */
{
double x;
double y;
double z;
} data;
} Coord;
/* This function will allocate an array of n+1 elements. The first element will
/* the title and number of points (n) while the remaining n elements will be
/* individual coordinates.
*/
Coord *allocCoordArray(int n)
{
Coord *retval;
int i=0;
retval = malloc((n+1) * sizeof(Coord));
if (NULL = = retval)
return NULL;
strcpy (retval[i].header.title, “unknown” ) ;;
retval[0].header.count = n;
for (i = 1; i < n+1; i++)
{
retval[i].data.x = 0.0;
retval[i].data.y = 0.0;
retval[i].data.z = 0.0;
}
free(retval);
return retval;
}
Indicate with reasons any problems you see with the code.
typedef union
{
struct /* Use this for the first array element*/
{
char title[20];
int count;
} header;
struct /* Use this for remaining array elements */
{
double x;
double y;
double z;
} data;
} Coord;
/* This function will allocate an array of n+1 elements. The first element will
/* the title and number of points (n) while the remaining n elements will be
/* individual coordinates.
*/
Coord *allocCoordArray(int n)
{
Coord *retval;
int i=0;
retval = malloc((n+1) * sizeof(Coord));
if (NULL = = retval)
return NULL;
strcpy (retval[i].header.title, “unknown” ) ;;
retval[0].header.count = n;
for (i = 1; i < n+1; i++)
{
retval[i].data.x = 0.0;
retval[i].data.y = 0.0;
retval[i].data.z = 0.0;
}
free(retval);
return retval;
}
Indicate with reasons any problems you see with the code.