string initilization WHY?

P

puzzlecracker

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));
}
 
M

Martin Ambuhl

And why should it not?

You really ought to specify the return type -- int -- for main(). You
can get away with leaving it out in C90 (where it is implicitly int),
but C99 does away with implicit int.

You really ought not omit returning a value (0, EXIT_SUCCESS, or
EXIT_FAILURE) from main. In C99 'return 0;' is implicit, but it isn't
in C90.
My question is COMPLETELY DIFFERN. I am asking why string is being
copied!!!!!!!!!!!!!!!!!!

It isn't! Why do you think it is? The address of the string literal
"Awesome" is being stored in the pointer str.
 
K

Keith Thompson

puzzlecracker said:
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.
 
S

Servé La

"Keith Thompson" <[email protected]> schreef in bericht

I saw this line.
printf("string:\'%s\' has %d characters \n", str, (int)strlen(str));

Having been in this group before I know that the cast to int is put in there
deliberately. But why is that?
It doesn't avoid any errors and it makes the code (a little) harder to read
I'd say
 
I

infobahn

Servé La said:
"Keith Thompson" <[email protected]> schreef in bericht

I saw this line.


Having been in this group before I know that the cast to int is put in there
deliberately. But why is that?

It's because we must be sure that the format specifier correctly
describes the type of its matching parameter. strlen returns a
size_t, not an int. Because the Standard doesn't give us a format
specifier for a size_t (in C90, at least), we must convert the
value into a type we know we can print.
It doesn't avoid any errors

Wrong. It avoids the error of sending an unmatchable type to a printf
call in a C90 program.
and it makes the code (a little) harder to read
I'd say

Yes, it does, but that's sometimes the price you pay for correctness.
(Often, it's the other way around; correct code is /more/ readable
than incorrect code.)
 
M

Mike Wahler

infobahn said:
It's because we must be sure that the format specifier correctly
describes the type of its matching parameter. strlen returns a
size_t, not an int. Because the Standard doesn't give us a format
specifier for a size_t (in C90, at least), we must convert the
value into a type we know we can print.

Correct. But I like to use 'unsigned long', which is the largest
unsigned integer type (common to C89 and C99), more likely to be able to
represent more of the range of 'size_t'. (Since C89 seems to still be
more prevalent, I still use it as my 'common denominator' for writing
C code).
Wrong. It avoids the error of sending an unmatchable type to a printf
call in a C90 program.


Yes, it does, but that's sometimes the price you pay for correctness.
(Often, it's the other way around; correct code is /more/ readable
than incorrect code.)

If you find the 'printf()' call too 'ugly', you could separate
the conversion into another statement:

unsigned long len = (unsigned long)strlen(str); /* put string length into
a 'printf-able' type */

printf("%lu\n", len); /* 'clean' call to printf()' */

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top