By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

string initilization WHY?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
"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.