Hi,
sorry for the newbie question; after reading the FAQ, I am still left
wondering if this is legal:
void strip (char *in)
{
in[strlen(in)-1] = '\0';
return;
}
I figure it is, because the memory location of the variable 'in' is
passed properly and I guess that the strlen() function seeks the '\0'
character to obtain its value. Is this true?
Thanks,
Andrej 16 2080
Andrej Prsa wrote: Hi,
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Yes, it's perfectly legal, and yes, that's how strlen works.
--
================================================== =============
In an attempt to reduce 'unwanted noise' on the 'signal' ...
Disclaimer:
Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - 'not 100% pedantically correct'].
I don't care too much about that though, and I reckon it's the
same with most 'visitors' here. However, rest assured that any
'essential' (?) corrections WILL almost certainly appear v.soon
[read - 'to add noise as they see fit, *a pedant* will be along
shortly'].
WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
================================================== =============
Andrej Prsa wrote: Hi,
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Thanks,
Andrej
void strip2 (char in[])
{
in[strlen(in)-1] = '\0';
return;
}
same as
void strip1 (char *in)
{
*(in+strlen(in)-1) = '\0';
return;
}
N.B.
char * s is not equivalent to char s[] as you can see with the following :
char string[32] = "litte string";
char *ptr = string;
sizeof (string) ==> 32
sizeof (ptr) ==> 4 /* (on a machine with 4 byte pointers) */
N.B.
in+strlen(in)-1 is not very nice because of the substract ...
it's not very meaningfull to substract with pointers ...
in this case it's like (in + (strlen(in)-1))
xavier
Andrej Prsa wrote: Hi,
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
The string `in` had better have at least one character in it, or `in`
must point one (or more) character(s) into legal storage. Because
strlen(in) == 0 would mean you had `in[-1]=0`.
BOOM today - if you're lucky. Otherwise, BOOM tomorrow.
--
Chris "understanding is a three-edged sword" Dollin
pemo wrote: Andrej Prsa wrote: sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Yes, it's perfectly legal, and yes, that's how strlen works.
================================================== ============= In an attempt to reduce 'unwanted noise' on the 'signal' ...
Disclaimer:
Any comment/code I contribute might =NOT= be 100% portable, nor semantically correct [read - 'not 100% pedantically correct']. I don't care too much about that though, and I reckon it's the same with most 'visitors' here. However, rest assured that any 'essential' (?) corrections WILL almost certainly appear v.soon [read - 'to add noise as they see fit, *a pedant* will be along shortly'].
WARNINGS: Always read the label. No beside-the-point minutiae filter supplied. Keep away from children. Do not ignite. ================================================== =============
how does a 16 line sig added to a one line comment "reduce noise"?
--
Nick Keighley
Andrej Prsa wrote: I am wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I am a newbie too, just want to point out somethings that could go wrong
with your function:
/* untested newbie code */
char *test;
test = NULL;
strip(test); /* BANG */
test = "";
strip(test); /* BANG */
test = "immutable string";
strip(test); /* BANG */
test = malloc(10);
if (test != NULL) {
strcpy(test, "");
strip(test); /* BANG */
}
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Yes, but maybe you should test for the various ways the function could
fail:
/* more newbie code */
void strip (char *in)
{
if (!in) return;
if (!*in) return;
in[strlen(in)-1] = '\0';
return;
}
I think there's something else about the function name:
isn't it reserved because it starts with "str" and continues with a
lowercase letter?
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
serrand wrote: Andrej Prsa wrote:
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
void strip2 (char in[]) { in[strlen(in)-1] = '\0'; return; }
same as
void strip1 (char *in) { *(in+strlen(in)-1) = '\0'; return; }
so what? How does the help the OP? His original code is clearer.
N.B. char * s is not equivalent to char s[] as you can see with the following :
char string[32] = "litte string"; char *ptr = string; sizeof (string) ==> 32 sizeof (ptr) ==> 4 /* (on a machine with 4 byte pointers) */
N.B. in+strlen(in)-1 is not very nice because of the substract ... it's not very meaningfull to substract with pointers ...
since when? This has been perfectly well defined in C since K&R
in this case it's like (in + (strlen(in)-1))
I don't see what the extra parens buy you.
--
Nick Keighley
Hi,
Thank you all very much for your very informative answers! :)
Best wishes,
Andrej
Pedro Graca wrote:
<snipped lots of code and good advice> I think there's something else about the function name: isn't it reserved because it starts with "str" and continues with a lowercase letter?
Yes, there is. Standard states that all function names (identifiers)
starting with `str` and having a lowercase letter as the fourth
character are reserved for future use by the standard library.
So:
strip() -- bad idea
strMyWord() -- OK
str_ip() -- OK
str2rts( ) -- OK
Cheers
Vladimir
Nick Keighley wrote: pemo wrote:
.... snip ... Yes, it's perfectly legal, and yes, that's how strlen works.
================================================== ============= In an attempt to reduce 'unwanted noise' on the 'signal' ...
Disclaimer:
Any comment/code I contribute might =NOT= be 100% portable, nor semantically correct [read - 'not 100% pedantically correct']. I don't care too much about that though, and I reckon it's the same with most 'visitors' here. However, rest assured that any 'essential' (?) corrections WILL almost certainly appear v.soon [read - 'to add noise as they see fit, *a pedant* will be along shortly'].
WARNINGS: Always read the label. No beside-the-point minutiae filter supplied. Keep away from children. Do not ignite. ================================================== =============
how does a 16 line sig added to a one line comment "reduce noise"?
Especially when it doesn't even have a proper sig marker.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Andrej Prsa wrote: Hi,
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Yes, it's legal code, with the following caveats:
If the string passed is read-only, the above won't work. (I don't
recall if it's UB or "implementation defined".)
char *pt = "This string may be read-only";
...
strip(pt);
(I don't think there's anything you can do about that, however, beyond
documenting "don't do that".)
Second, you don't handle the case of a zero-length string being passed.
char str[] = "";
...
strip(str);
Finally, you don't handle a NULL parameter.
strip(NULL);
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
CBFalconer <cb********@yahoo.com> wrote: Nick Keighley wrote: pemo wrote: ... snip ... Yes, it's perfectly legal, and yes, that's how strlen works.
================================================== ============= In an attempt to reduce 'unwanted noise' on the 'signal' ...
how does a 16 line sig added to a one line comment "reduce noise"?
Especially when it doesn't even have a proper sig marker.
It did before Nick quoted it. That's about the only good thing about it.
Richard
On 26 Jan 2006 02:16:24 -0800, "Nick Keighley"
<ni******************@hotmail.com> wrote: pemo wrote:
<snip WARNINGS: Always read the label. No beside-the-point minutiae filter supplied. Keep away from children. Do not ignite. ================================================== =============
how does a 16 line sig added to a one line comment "reduce noise"?
It worked for me - I killfiled him several days ago.
--
Al Balmer
Sun City, AZ
Andrej Prsa <an*********@fmf.uni-lj.si> writes: sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
The function assumes that 'in' points to a string containing at least
one character. If you know that it always does, and it doesn't matter
what that last character is (I guess it's '\n' you're after), the
function seems fine to me, so it's legal and even arguably correct.
I would give it a more descriptive name, though.
If I wrote a function to strip newlines, I would check for the
existence of the newline at the end and let the string be unchanged if
there was no newline there.
The explicit "return" is not needed - the function ends there anyway.
Pedro Graca <he****@dodgeit.com> writes: Andrej Prsa wrote: I am wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I am a newbie too, just want to point out somethings that could go wrong with your function: test = "immutable string"; strip(test); /* BANG */
This one can often be made to give rise to a warning by the compiler.
With gcc, use -Wwrite-strings, and then interpret the text appropriately:
% gcc -Wwrite-strings strw.c
strw.c: In function `main':
strw.c:9: warning: initialization discards qualifiers from pointer target type
(If you want to make the string constant actually writable, the compiler
may provide an option for that too, but then it's not portable C.)
Kenneth Brody <ke******@spamcop.net> writes: Andrej Prsa wrote: sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true? Yes, it's legal code, with the following caveats:
If the string passed is read-only, the above won't work. (I don't recall if it's UB or "implementation defined".)
char *pt = "This string may be read-only"; ... strip(pt);
(I don't think there's anything you can do about that, however, beyond documenting "don't do that".)
I think that's the key to the whole thing. We can discuss what the
function will do (or how it will fail) given various arguments, but
what it really needs is documentation, a specification of what it's
*supposed* to do.
Second, you don't handle the case of a zero-length string being passed.
char str[] = ""; ... strip(str);
That's acceptable if the specification says not to pass it a
zero-length string -- but it would probably be better for the function
to do nothing in that case.
Finally, you don't handle a NULL parameter.
strip(NULL);
It's common for string functions to invoke undefined behavior if
called with a null pointer (see <string.h>), so that's not much of a
problem -- but again, it should be documented.
As others have mentioned the name "strip" is reserved. This is a bit
off-topic, but Perl has a built-in function "chop" that deletes the
last character of a string, and another built-in function "chomp" that
deletes the last character only if it's a newline; if I wanted to
write an equivalent C function, I'd probably use the same name.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
pemo wrote: Andrej Prsa wrote:
Hi,
sorry for the newbie question; after reading the FAQ, I am still left wondering if this is legal:
void strip (char *in) { in[strlen(in)-1] = '\0'; return; }
I figure it is, because the memory location of the variable 'in' is passed properly and I guess that the strlen() function seeks the '\0' character to obtain its value. Is this true?
Yes, it's perfectly legal, and yes, that's how strlen works.
char in[] = "hello";
012345 Indexes into the string. in[5] is the '\0'.
strlen(n) is 5.
in[5-1] = '\0'; Will overwrite 'o' resulting in "hell".
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tim Johansson |
last post by:
I'm new to C++, and tried to start making a script that will shuffle an
array. Can someone please tell me what's wrong?
#include <iostream.h>
#include <string.h>
int main () {...
|
by: Vaca Louca |
last post by:
Hello,
I write an ISAPI authentication module which uses Berkeley DB and
want it to be as efficient as possible.
Both ISAPI and BerkeleyDB use arrays of chars (char *)
to pass and receive...
|
by: andrew.fabbro |
last post by:
In a different newsgroup, I was told that a function I'd written that
looked like this:
void myfunc (char * somestring_ptr)
should instead be
void myfunc (const char * somestring_ptr)
...
|
by: SB |
last post by:
What is the proper way to pass a character array (char *) from a "C" dll to
a C# method (delegate) in my app? Getting the dll (which simulates a third
party dll) to call my delegate works fine. ...
|
by: max |
last post by:
Dear all,
I did the following analysis to conclude that the following pointer
types are not compatible. Please let me know If my analysis and
interpretation of the C standard are correct:
...
|
by: Superman859 |
last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well
in creating a Java program with very few syntax errors, but I get them
all over the place in c++. The smallest little things...
|
by: Elikhom |
last post by:
Hi,
I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is...
|
by: Inner Dragon |
last post by:
Hey guys, I've been having a real hard time trying to pass a string as
parameter and then converting it to char to be used with
SDLNet_TCP_Recv. It's been hell with me though, here's my non-working...
|
by: MatejSlansky |
last post by:
Hi everybody,
I'd like to ask for help with usage of char **. I have a following func, which dynamically allocates pointer a array
and pointers of char arrays in it:
int sptr(char **ppchr)
{...
|
by: randysimes |
last post by:
I am having a user enter two strings: user, password. I then have to pass these two strings to a class function that accepts const char* for user and char* for password. How would I convert the...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |