Sunday, August 2, 2009

Need help with some programing in C?

can any one help me program these functions in C





N = readdata(%26amp;A). This function reads input data from stdin into the given array. readdata() allocates memory to hold all the data and returns the number of items read.


. z = compare(x1,x2). This function returns a number less than zero, equal to zero, or greater than zero depending on whether x1%26lt;x2, x1==x2, or x1%26gt;x2 respectively.


the prototypes are


int readdata(char ***a)


int compare (char *x, char *y)

Need help with some programing in C?
Readdata allows up to 4095 bytes per newline separated record. It does not do any upper bound checking on the total number of records it will read, so if you pipe in a large file it will likely cause some paging. It will realloc when it needs more data, but there is no error checking if realloc() fails; better add that.





The small main to test reverses the input records to stdout.








#include %26lt;unistd.h%26gt;


#include %26lt;stdio.h%26gt;





int compare( char x, char y )


{


return x - y;


}





int readdata( char ***a )


{


char **d = NULL;


char nalloc = 0;


char idx = 0;


char buf[4096];





while( fgets( buf, sizeof( buf ), stdin ) )


{


if( idx %26gt;= nalloc )


{


nalloc += 100;


d = (char **) realloc( d, sizeof( char *) * nalloc );


}





d[idx++] = strdup( buf );


}





*a = d;


return idx;


}





main()


{





int i;


char **a;





i = readdata( %26amp;a );


while( i %26gt; 0 )


printf( "%s", a[--i] );


}


No comments:

Post a Comment