Re: string initilization WHY?

From: Keith Thompson (kst-u_at_mib.org)
Date: 01/30/05


Date: Sun, 30 Jan 2005 08:33:58 GMT


"puzzlecracker" <ironsel2000@gmail.com> writes:
> Why would this work? - and it does! any thoughts?
>
>
>
> #include<stdio.h>
>
> void init(char **str){ *str="Awesome"; }
>
> main(){
>
>
> char * str;
>
> printf("address: %d\n", str);
> init(&str);
> printf("address: %d\n", str);
> printf("string:\'%s\' has %d characters \n", str,strlen(str));
> }

It "works" because behaving as you expect is one of the infinitely
many possible consequences of undefined behavior.

Here's a corrected version of your program that (almost) avoids the
undefined behavior:

#include <stdio.h>
#include <string.h>

void init(char **str){ *str="Awesome"; }

int main(void)
{
    char *str;
    printf("address: %p\n", (void*)str);
    init(&str);
    printf("address: %p\n", (void*)str);
    printf("string:\'%s\' has %d characters \n", str, (int)strlen(str));
    return 0;
}

I say "almost" because it still uses the value of an unitialized (and
therefore indeterminate) variable in the first printf() call.

You could probably get away without the void* casts, since void* and
char* are effectively compatible as parameters, but IMHO it's good
style to use it anyway. (I've argued before that all casts should be
considered suspicious. Arguments to printf-like functions are a rare
case where casts are often required, because there is no implicit
conversion to the proper type.)

But as you said elsewhere in this thread, none of the problems I
corrected are directly relevant to what you're asking about.

The variable str is a pointer to char; effectively it points to a
string. The init() call changes the value of str, so it points to a
string whose value is "Awesome" (with a trailing '\0', of course).
(The string itself isn't copied.) This string is statically
allocated, so there's no problem referring to it after leaving the
init() function.

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


Relevant Pages

  • Re: Reading a large number of text files into an array
    ... > convert this string into an array of doubles? ... > int main ... the array dependent on the maximum length of all components from which the ... No need for either of the casts here. ...
    (comp.lang.c)
  • Re: Delphi can do map/reduce?
    ... TIntegerArray = array of Integer; ... TStringArray = array of String; ... function IntReduce(fn: TMapableIntFunc; a: TIntegerArray; init: Integer): Integer; ... By itself this doesn't really do much, but rewritting the IntReduce and StrReduce function to a multithreaded or message disbatching to server farm approach, would make this alot more interesting. ...
    (borland.public.delphi.non-technical)
  • Re: C# Conversion Differences
    ... are several kinds of casts in C#. ... you have a value type (such as int) wrapped up to ... Conversions down the inheritance hierarchy. ... Converting a string to a short is complex. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Xilinx/Synplicity LUT Placement
    ... it accepted a boolean equation as a string for input and returned the hex string for the LUT init as the output. ... If you can't get the xc_map to work out for verilog, then perhaps you can use a boolean equation to INIT string parser function to do that conversion. ... instantiating LUTs and using the INITs directly is not a fun way to do any design. ... localparam I0 = 16'haaaa; ...
    (comp.arch.fpga)
  • Modem init sequence in KPPP
    ... and my modem won't connect at more than 28 KB. ... The init string in KPPP is not right. ...
    (Debian-User)