Author |
Message |
Faisal Nasi #1 / 21
|
 pointer arithmetic
how do i remove the last character by using pointer arithmetic? I need to call a function which takes a variable as argument, and removes its last character. is that possible? how? Please reply as email. Thank you.
|
Mon, 13 Nov 2000 03:00:00 GMT |
|
 |
Nicolas Thomassi #2 / 21
|
 pointer arithmetic
Maybe I misunderstood the meaning of "pointer arithmetic" but it seem to do the job. BTW, this function expect to receive a null terminated string. void RemoveLastCharacter (char *thisString) { int nLen; nLen = strlen (thisString); /* Replace the last character in the string by a null */ if (nLen != 0) *(thisString + nLen - 1) = '\0'; Quote: }
Good luck! Quote:
> how do i remove the last character by using pointer arithmetic? > I need to call a function which takes a variable as argument, > and removes its last character. > is that possible? how? > Please reply as email. > Thank you.
-- Nicolas Thomassin
|
Mon, 13 Nov 2000 03:00:00 GMT |
|
 |
firewin #3 / 21
|
 pointer arithmetic
Quote:
> how do i remove the last character by using pointer arithmetic?
str[strlen(str)] = '\0'; Or, if you want to make the 'arithmetic' part clearer: *(str + strlen(str)) = '\0'; -- (initiator of the campaign for grumpiness where grumpiness is due in c.l.c) Attempting to write in a hybrid which can be compiled by either a C compiler or a C++ compiler produces a compromise language which combines the drawbacks of both with the advantages of neither.
|
Mon, 13 Nov 2000 03:00:00 GMT |
|
 |
Martin Ambuh #4 / 21
|
 pointer arithmetic
:>> how do i remove the last character by using pointer arithmetic? :> str[strlen(str)] = '\0'; :>Or, if you want to make the 'arithmetic' part clearer: :> *(str + strlen(str)) = '\0'; :How about: : : str[strlen(str)-1] = '\0'; : :instead. ============= While wrong, firewind's example has the virtue of having defined behavior when strlen(str) == 0. ============= : :-- glen
|
Mon, 13 Nov 2000 03:00:00 GMT |
|
 |
firewin #5 / 21
|
 pointer arithmetic
Quote:
> > str[strlen(str)] = '\0'; > str[strlen(str)-1] = '\0';
Er... right. I can remember that 'str[strlen(str)]' is '\0' by definition. Really I can. -- (initiator of the campaign for grumpiness where grumpiness is due in c.l.c) *thunk*
|
Mon, 13 Nov 2000 03:00:00 GMT |
|
 |
G. Herrmannsfel #6 / 21
|
 pointer arithmetic
Quote:
>> how do i remove the last character by using pointer arithmetic? > str[strlen(str)] = '\0'; >Or, if you want to make the 'arithmetic' part clearer: > *(str + strlen(str)) = '\0'; >-- >(initiator of the campaign for grumpiness where grumpiness is due in c.l.c) >Attempting to write in a hybrid which can be compiled by either a C compiler >or a C++ compiler produces a compromise language which combines the drawbacks >of both with the advantages of neither.
How about: str[strlen(str)-1] = '\0'; instead. -- glen
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Joe Mau #7 / 21
|
 pointer arithmetic
Quote:
> > how do i remove the last character by using pointer arithmetic? > str[strlen(str)] = '\0';
This would remove the last character all right, it would remove the null terminator. (A bit redundant, i'd say :) Quote: > Or, if you want to make the 'arithmetic' part clearer: > *(str + strlen(str)) = '\0'; > --
Joe
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Andy Knig #8 / 21
|
 pointer arithmetic
#include <string.h> void removeLastChar(char* string) { if (*string != '\0') strchr(string, '\0')[-1] = '\0' ; Quote: }
--- On Thu, 28 May 1998 14:12:28 +0500, "Faisal Nasim" Quote:
>how do i remove the last character by using pointer arithmetic? >I need to call a function which takes a variable as argument, >and removes its last character. >is that possible? how? >Please reply as email. >Thank you.
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Szu-Wen Hua #9 / 21
|
 pointer arithmetic
[...] : >> how do i remove the last character by using pointer arithmetic? : > str[strlen(str)] = '\0'; This one is outright wrong. [...] : str[strlen(str)-1] = '\0'; This one is better, but still wrong because it doesn't handle zero- length strings. The following should work, assuming that s != NULL. if (*s) { s[strlen(s)-1] = 0; }
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Lew Pitch #10 / 21
|
 pointer arithmetic
Quote:
>[...] >: >> how do i remove the last character by using pointer arithmetic? >: > str[strlen(str)] = '\0'; >This one is outright wrong. >[...] >: str[strlen(str)-1] = '\0'; >This one is better, but still wrong because it doesn't handle zero- >length strings. The following should work, assuming that s != NULL. > if (*s) > { > s[strlen(s)-1] = 0; > }
Good, but let's make it bulletproof, assuming that s may == NULL char *s; /* pointer to string, or NULL */ /* some code skipped */ if ( s != NULL && *s) s[strlen(s)-1] = 0; Now, let's try that with pointer arithmetic only (no array/pointer translation) char *s; /* pointer to string, or NULL */ /* some code skipped */ if ( s != NULL && *s) *(s+(strlen(s)-1)) = 0; Lew Pitcher System Consultant, Delivery Systems Architecture Toronto Dominion Bank (Opinions expressed are my own, not my employer's.)
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
John Kugelma #11 / 21
|
 pointer arithmetic
Quote:
> >[...] > >: >> how do i remove the last character by using pointer arithmetic? > >: > str[strlen(str)] = '\0'; > >This one is outright wrong. > >[...] > >: str[strlen(str)-1] = '\0'; > >This one is better, but still wrong because it doesn't handle zero- > >length strings. The following should work, assuming that s != NULL. > > if (*s) > > { > > s[strlen(s)-1] = 0; > > } > Good, but let's make it bulletproof, assuming that s may == NULL > char *s; /* pointer to string, or NULL */ > /* some code skipped */ > if ( s != NULL && *s) s[strlen(s)-1] = 0;
Still not bullet proof. If /* some code skipped */ doesn't initialize s, or if it malloc's and then free's s, the comparison to NULL will cause undefined behavior. Of course, since you can't do anything about those cases, it was probably silly of me to point them out. --
I believe we can change anything. I believe in my dream. - Joe Satriani
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Bruce Weddi #12 / 21
|
 pointer arithmetic
Quote: >#include <string.h> >void removeLastChar(char* string) >{ > if (*string != '\0') > strchr(string, '\0')[-1] = '\0' ; >}
Sure hope string isn't a NULL pointer. Bruce Wedding Software Manager
Scientific MicroSystems, Inc. http://www.scimisys.com Tomball, Texas
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Richard Sta #13 / 21
|
 pointer arithmetic
Quote:
>Good, but let's make it bulletproof, assuming that s may == NULL > if ( s != NULL && *s) s[strlen(s)-1] = 0;
Indeed, though we now have s being explicitly compared with NULL as against *s standing alone. There's nothing wrong with that, but wouldn't you say it looked a bit inconsistent? How about one of if (s && *s) { s[strlen(s)-1] = 0; } if (s != NULL && *s != 0) { s[strlen(s)-1] = 0; } Quote: >Now, let's try that with pointer arithmetic only (no array/pointer translation) > if ( s != NULL && *s) *(s+(strlen(s)-1)) = 0;
But this adds obfuscation and gains nothing. There's occasionally an efficiency argument for *iterating* over an array with a pointer (though modern compilers make even that dubious) but there's no benefit when you're just dipping into the array once, not least because the two expressions are defined to be equivalent. [Of course, constructing this kind of expression can be a worthwhile *exercise*, I'm just saying it's not the best practice in actual code.] Bet whoever posted the original question is sorry he/she asked. :-) Cheers, Richard -- Richard Stamp Churchill College, Cambridge
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
G. Herrmannsfel #14 / 21
|
 pointer arithmetic
Quote:
>[...] >: >> how do i remove the last character by using pointer arithmetic? >: > str[strlen(str)] = '\0'; >This one is outright wrong. >[...] >: str[strlen(str)-1] = '\0'; >This one is better, but still wrong because it doesn't handle zero- >length strings. The following should work, assuming that s != NULL. > if (*s) > { > s[strlen(s)-1] = 0; > }
Well, a zero length string doesn't have a last character, so it is not included in the problem definition. Still, how about if(*s) s[strlen(s)-1] = 0; Instead. Why take four lines for such a simple function? -- glen
|
Tue, 14 Nov 2000 03:00:00 GMT |
|
 |
Lawrence Kir #15 / 21
|
 pointer arithmetic
Quote:
>>#include <string.h> >>void removeLastChar(char* string) >>{ >> if (*string != '\0') >> strchr(string, '\0')[-1] = '\0' ; >>} >Sure hope string isn't a NULL pointer.
If string is a null pointer then it isn't a pointer to a string which means that there is an error in the calling code. Testing fo a null pointer like this won't fix that error, if anything it will just make it more difficult to track down. If you want to test for a null pointer argument a good way to do it would be to use assert(). Functions like strlen() and strcpy() don't test for null arguments, there's no need to make this one inconsistent. -- -----------------------------------------
-----------------------------------------
|
Wed, 15 Nov 2000 03:00:00 GMT |
|
|