Truncating char array

Q

qazmlp

Can anybody comment(& suggest improvements) on the following
implementation that is for
truncating the character buffer contents to the desired length?

Truncating char array
void truncateCharArray(int maxLength , char * buffer )
{
if ((0 == buffer) || !(0 < maxLength))
return;
if (maxLength < strlen(buffer))
{
buffer[maxLength] = '\0';
}
return;
}

Are there any problems in this?
 
R

Robert B. Clark

Can anybody comment(& suggest improvements) on the following
implementation that is for
truncating the character buffer contents to the desired length?

Truncating char array
void truncateCharArray(int maxLength , char * buffer )
{
if ((0 == buffer) || !(0 < maxLength))
return;

To my eye, this is confusing. Why not simply

if (buffer == NULL || maxLength <= 0)
return;
if (maxLength < strlen(buffer))

What if maxLength is equal to the length of buffer?
{
buffer[maxLength] = '\0';
}
return;
}

Moreover, you should use size_t for the requested string length instead of
an int. Also, it would be useful to have your function return some type of
indication of success or failure.

Consider the following version of your code:

#include <string.h>

size_t truncateCharArray(size_t maxLength, char *buffer)
{
/* NULL string or illegal requested length? */

if (buffer == NULL || !maxLength)
return 0;

/* Truncate string; return length of truncated string or 0 if error */

if (maxLength <= strlen(buffer))
{
buffer[maxLength] = '\0';
return maxLength;
}
else
return 0;
}
 
R

Richard Bos

Al Bowers said:
Actually you would make it:
if(buffer == NULL || 0 > maxLength)
A maxLength argument of 0 would make an empty string.

_I_ would, but I was assuming identical semantics to the original
function here. And note that I did ask for the intentionality of this
oddity at the end of my post.
But still there is no need for maxLength to represent a
negative number, so I would make maxLength type unsigned.

Even better.

Richard
 
Q

qazmlp

Can anybody comment(& suggest improvements) on the following
implementation that is for
truncating the character buffer contents to the desired length?

Truncating char array
void truncateCharArray(int maxLength , char * buffer )
{
if ((0 == buffer) || !(0 < maxLength))

Why not just write maxLength <= 0? The way you've done it is
suboptimally disobfuscatory.
You don't need the parens around buffer == 0 either.
return;
if (maxLength < strlen(buffer))
{
buffer[maxLength] = '\0';
} return;
}

Are there any problems in this?

Not really, apart from the odd way of phrasing. Technically it's
correct. However, it could be written more concisely and IMO more
clearly as

void truncateCharArray(int maxLength , char * buffer )
{
if (buffer!=NULL && maxLength>0 && maxLength<strlen(buffer))
buffer[maxLength] = '\0';
return;
}

Note that, as written, this function won't truncate a string to zero
characters. Was this intentional?
Yes. I want the string to get truncated to 'maxLength' characters.

I need 2 more clarifications:
- Say the buffer contains 10 characters(including null character).
Now, if I make, "buffer[5] = '\0';", what will happen to the remaining
chracters after the position where the '\0' character is inserted.
When I tested, I could print those characters. Is that an expected
behaviour ? If yes, aren't there side effects when I use this buffer
for further operations ?

- Which one is portable among the following?
if( buffer != NULL )
or
if( buffer != 0 )
Why?
 
R

Richard Bos

Not really, apart from the odd way of phrasing. Technically it's
correct. However, it could be written more concisely and IMO more
clearly as

void truncateCharArray(int maxLength , char * buffer )
{
if (buffer!=NULL && maxLength>0 && maxLength<strlen(buffer))
buffer[maxLength] = '\0';
return;
}

Note that, as written, this function won't truncate a string to zero
characters. Was this intentional?
Yes. I want the string to get truncated to 'maxLength' characters.

Yes; and if maxLength is zero, it won't perform the truncation. To me,
that sounds like an odd specification, but you know your own intentions
best.
I need 2 more clarifications:
- Say the buffer contains 10 characters(including null character).
Now, if I make, "buffer[5] = '\0';", what will happen to the remaining
chracters after the position where the '\0' character is inserted.

Nothing whatsover.
When I tested, I could print those characters. Is that an expected
behaviour ?

Yes. Memory you don't change remains unchanged (barring volatile
objects, of course). What is so surprising about that?
If yes, aren't there side effects when I use this buffer
for further operations ?

What kind of side effects were you expecting?
- Which one is portable among the following?
if( buffer != NULL )
or
if( buffer != 0 )
Why?

Both. Both NULL and 0 are null pointer constants; in a pointer context
(and a comparison to buffer, which is a pointer, _is_ a pointer context)
both will be converted to a null pointer, and both will check that
buffer is not null. In this context, they are equivalent.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top