Re: return a string



"Emmanuel Delahaye" <emdel@xxxxxxxxxxxxxx> writes:
> Nascimento wrote on 29/04/05 :
[...]
>> for( i = 0; i < num-1; i++ )
>> strcpy(tralha, strcat(tralha,"#"));
>
> Note that this is dangerous. The order of execution of the parameters
> is defined by the implementation. BTW, chances are that strcat() is
> called before strcpy().

Actually, I think it's guaranteed that strcat() will be called before
strcpy(). The order of evaluation of the arguments is unspecified,
but the function can't be called until all its arguments have been
evaluated; there's a sequence point between the evaluation of the
arguments and the actual call. It's bad code, but not for that
reason.

> Additionally, strcpy() only works with non
> overlapping strings. Your construction doesn't guarantee that.

In fact, it guarantees that the strings do overlap.

[...]

> As a rule of thumb, don't use functions as a parameter to another
> function. In mosts cases, it hurts...

I don't agree. It can be dangerous if you're dealing with pointers to
the same chunk of memory, but there's nothing wrong with using
function results as arguments in general.

printf("Length of string is %d\n", (int)strlen(s));

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.



Relevant Pages

  • Re: Is strcat in disgrace by Tcl sources?
    ... > - Am I right that strcat has been disparaged? ... Tcl uses Tcl_DString, ... automatically uses fixed storage for small strings and dynamic storage ... when strings get larger. ...
    (comp.lang.tcl)
  • Re: Efficient strcat implementation.
    ... Programmers using the `strcat' or `wcscat' function (or the ... /* This function concatenates arbitrarily many strings. ... concat (const char *str, ...) ... if (newp == NULL) ...
    (comp.lang.c)
  • Re: Cant import
    ... I'm using Corman Lisp. ... (defun strcat (&rest strings) ... "Concatenate strings together" ... a) It's much more convenient to get access to all exported symbols of a package at once, and b) almost all important package-related functionality can be better used in a more declarative way using the 'defpackage macro. ...
    (comp.lang.lisp)
  • Re: Dictionary self lookup
    ... The reference to original composing strings is lost at ... It's called Lazy Evaluation and there are a bunch of tools that do it ... special blocks that tell your parser that they are references to other ...
    (comp.lang.python)
  • Re: segfault using getch & strcat
    ... strcat() expects two strings as arguments. ... strings are null-terminated, and let isn't. ... and you did allocate space for pass and properly null-terminate it ... int length = 0; ...
    (comp.unix.programmer)