T
Thad Smith
pkirk25 said:#define STRING_MAX_SIZE 254 /* ample headroom here */
/*
Function that
*/
You may be truncating for Usenet, but I consider the function header
to be one of the most important parts of my code. It is the contract
that I meet, a reference point to determine if a problem is incorrect
function implementation or incorrect usage. My suggestion is to take
advantage of that opportunity.
int FixString(const char *strIn, /* string to fix */
const char cToken, /* token to use */
char *strOut) /* the fixed string */
{
int i = 0;
int j = strlen(strIn);
if (j > STRING_MAX_SIZE)
{
printf("Input too big\n");
return 1;
}
While this works, my suggestion is since the maximum string length limit
is determined by the size of the array passed to strOut, the caller
should specify the length to the function, not code an arbitrary length
between the function and its caller. If a size parameter is needed, it
could be added as an additional parameter. In this case, it might be
better to have the caller verify that the input string is not longer
than the output string and stipulate that as an input requirement to use
the function.
Also, if an error is detected in a function, I prefer to pass an error
code to the caller and let the caller print an error message if
suitable. This makes the function more generic and able to work in
different environments where printf output may not be suitable. Also,
the caller has more context about the error to be able to give more
useful information. For example, in the case, strIn is simply a pointer
to a string, which could be used for many different things. The caller
should know what the string is used for, which might be a file name.
Therefore the caller might issue a message to stderr that the output
file name is too long.