473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Truncating char array

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
4 21911
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
4
by: rajdb2 | last post by:
Hi, I am using the following sql statement SELECT rtrim(rtrim(coalesce(substr(char(v.creationdate),1,4) || '-' || substr(char(v.creationdate),6,2) || '-' || substr(char(v.creationdate),9,2) ||...
3
by: qazmlp | last post by:
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...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
4
by: DotNetJunkies User | last post by:
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
3
by: Sonnich | last post by:
Hi all! I have 2 arrays of parts and assemblies. I check the part array whether there is an assembly part in there, if so, 1) it should be moved to the assembly array 2) its parts should be...
3
by: rlrcstr | last post by:
Is it possible to truncate a MemroyStream to free up the space up to the current position? I wantes to be able to just keep dumping bytes into a stream and then read the stream as needed. After...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.