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 truncateCharArr ay(int maxLength , char * buffer )
{
if ((0 == buffer) || !(0 < maxLength))
return;
if (maxLength < strlen(buffer))
{
buffer[maxLength] = '\0';
}
return;
}
Are there any problems in this? 4 21984
On 25 Jul 2003 03:14:55 -0700, qa********@redi ffmail.com (qazmlp) wrote: Can anybody comment(& suggest improvements) on the following implementati on that is for truncating the character buffer contents to the desired length?
Truncating char array void truncateCharArr ay(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 truncateCharArr ay(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/
Al Bowers <xa*@abowers.co mbase.com> wrote: Richard Bos wrote:
qa********@redi ffmail.com (qazmlp) wrote:
Truncating char array void truncateCharArr ay(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 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 truncateCharArr ay(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 truncateCharArr ay(int maxLength , char * buffer ) { if (buffer!=NULL && maxLength>0 && maxLength<strle n(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(incl uding 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? qa********@redi ffmail.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 truncateCharArr ay(int maxLength , char * buffer ) { if (buffer!=NULL && maxLength>0 && maxLength<strle n(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(incl uding 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ying Yang |
last post by:
Hi,
whats the difference between:
char* a = new char;
char* b = new char;
char c ;
|
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) || ' ' ||
substr(char(v.creationdate),12,2) || ':' ||
substr(char(v.creationdate),15,2) || ':' ||
substr(char(v.creationdate),18,2), '')) ||'~'
|| rtrim(coalesce(substr(char(v.lastmodifieddate),1,4) || '-' ||
|
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 truncateCharArray(int maxLength , char * buffer )
{
if ((0 == buffer) || !(0 < maxLength))
return;
if (maxLength < strlen(buffer))
|
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 variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var, var is actually a memory address (or at
|
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 following program:
#include<stdio.h>
#define SIZE 1
int main()
| |
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 HEX value of 4E 31 00 00 01 00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to Vb.net all I have is 4E 31 or N1. Now I can return the same string as a return value of a function and I get it all.
Please Help,
Patrick Horn...
|
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 of bytes.
For example, the .lib contains this function:
int create(int id, int scale, unsigned char *image);
In the wrapper DLL I have this function:
|
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 added to the parts array 3)
it should be removed from the parts array.
My problem is to delete from the assy array, ot truncate it. I am used
to Delphi, where the is a SetLenght function.
|
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 the read, free up the portion of the stream I
read, esentially making the first unread byte the first byte in the stream.
Thanks.
Jerry
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |