473,473 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 21921
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.