468,256 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

string initilization WHY?

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

Nov 14 '05 #1
7 1299

puzzlecracker wrote:
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));
}


read this: http://www.ishiboo.com/~nirva/c++/stl-ref/lib_prin.html
There is more to printf than %d. There is more to C than printf.

Nov 14 '05 #2

Kobu wrote:
puzzlecracker wrote:
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));
}


read this: http://www.ishiboo.com/~nirva/c++/stl-ref/lib_prin.html
There is more to printf than %d. There is more to C than printf.


My question is COMPLETELY DIFFERN. I am asking why string is being
copied!!!!!!!!!!!!!!!!!!

Nov 14 '05 #3
puzzlecracker wrote:
puzzlecracker wrote:
Why would this work? - and it does! any thoughts?
And why should it not?
#include<stdio.h>
void init(char **str){ *str="Awesome"; }
main(){
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.


char * str;

printf("address: %d\n", str);
init(&str);
printf("address: %d\n", str);
printf("string:\'%s\' has %d characters \n", str,strlen(str));
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.

Nov 14 '05 #4
"puzzlecracker" <ir*********@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) 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.
Nov 14 '05 #5

"Keith Thompson" <ks***@mib.org> schreef in bericht
news:ln************@nuthaus.mib.org...

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
Nov 14 '05 #6
"Servé La" wrote:

"Keith Thompson" <ks***@mib.org> schreef in bericht
news:ln************@nuthaus.mib.org...

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'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.)
Nov 14 '05 #7
"infobahn" <in******@btinternet.com> wrote in message
news:41***************@btinternet.com...
"Servé La" wrote:

"Keith Thompson" <ks***@mib.org> schreef in bericht
news:ln************@nuthaus.mib.org...

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'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).
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.)


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
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Krakatioison | last post: by
5 posts views Thread by Stu Cazzo | last post: by
9 posts views Thread by John F Dutcher | last post: by
20 posts views Thread by Pierre Fortin | last post: by
9 posts views Thread by Derek Hart | last post: by
10 posts views Thread by Angus Leeming | last post: by
2 posts views Thread by Andrew | last post: by
6 posts views Thread by puzzlecracker | last post: by
5 posts views Thread by vsnadagouda | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.