By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

Truncating char array

P: n/a
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?
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 25 Jul 2003 03:14:55 -0700, qa********@rediffmail.com (qazmlp) wrote:
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;
}

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #2

P: n/a
Al Bowers <xa*@abowers.combase.com> wrote:
Richard Bos wrote:
qa********@rediffmail.com (qazmlp) wrote:
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.


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
Nov 13 '05 #3

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<3f****************@news.nl.net>...
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?
Nov 13 '05 #4

P: n/a
qa********@rediffmail.com (qazmlp) wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<3f****************@news.nl.net>...
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
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.