Re: return a string



muzgocrust@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (iru_muzgo) writes:
> Nascimento,
> you cannot assign a char to a string like that:
> tralha = "#"
> you can do it like this: tralha[0]='#';
> A more elegant code for your purpose goes like this:
>
> --- tralha.c ---
>
> void
> ptr_tralha(char *s)
> {
> while (*s != '\0') /* while the contents of s are different
> * from NULL */

No. NULL is (a macro that expands to) a null *pointer* constant.
'\0' is a null character, sometimes referred to as NUL. Using the
term NULL to refer to a character value is misleading.

> *s++ = '#'; /* s = '#' . increment s */
> }
>
>
> int
> main(void)
> {
> char s[5];
> ptr_tralha(s);
> puts(s);
>
> return 0;
> }

Your array s is not initialized before you pass its address to
ptr_tralha(). Inside ptr_tralha(), you loop over the array until you
find a '\0' character, but there's no reason to assume that you ever
will. Unless there happens to be a '\0' character somewhere within s,
the loop in ptr_tralha() will go past the end of the array, invoking
undefined behavior.

I just tried compiling and running your program, and it printed a
string of 5 '#' characters followed by a newline. Apparently there
just happened to be no '\0' characters in s itself, but there just
happened to be a '\0' character immediately following it in memory.
This is just one of the infinitely many possible consequences of
undefined behavior.

--
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

  • Help in Spanish translation of the description of UDFs
    ... functions of minimum / maximum values among elements of an array column. ... GETALLWORDS- Inserts the words from a string into a global dimensioned ... WORDTRAN- Searches a character string for occurrences of a first word, ... ARRAYSUM- Returns the sum of all or a specified range of numeric (and/or ...
    (microsoft.public.fox.helpwanted)
  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • Re: Check for Common character sequence ( I will pay)?
    ... Do I need to return an array? ... You need to identify character sequences of 3 or more characters that appear ... in more than one string. ... and test each 3-character sequence that results. ...
    (microsoft.public.dotnet.framework)
  • Re: The value of variables
    ... I write a programe as follows,and compile successfuly. ... end of an array invokes undefined behavior after which *anything* is ... including the nul character. ...
    (comp.lang.c)
  • Re: Desirable Usage of Fortran Modules
    ... suppose we want to cast a character array as ... A function that returns a string ... array of double precision numbers: ...
    (comp.lang.fortran)