On 5/13/09 Wed May 13, 2009 4:48 AM, "Michael Alipio"
<[email protected]> scribbled:
> I have a c code that looks like this:
>
> #include<stdio.h>
>
> main (){
> char girl[] = "anna";
> char boy[] = "jude";
> stringcopy(boy, girl); /* copy boy to girl */
> printf("%s", girl);
>
> }
>
> void stringcopy(char *b, char *g){
>
> while ((*g++ = *b++) != '\0')
> ;
> }
>
>
> It prints fine...
> However if I replace the stringcopy call arguments with "jude", "anna"
> it compiles fine but i get segmentation fault when running.
>
>
> How come printf can accept variable names as well as constant strings such as:
>
> printf ("%s", girl);
>
> and
>
> printf ("Hello World\n");
Because printf does not attempt to change its arguments.
> My stringcopy function only accepts pointers. Shouldn't I be passing pointer
> to the first element of "anna" when passing the string constant "anna"?? )
stringcopy modifies its second argument. Your compiler is not letting you
modify a string "constant". That way, different parts of your program can
share the same string constant without one part being affected by what
another part does.
> How does printf print a string constant then?
Easily, because it does not attempt to modify it.
May I ask you a question? Why are you posting C questions to a Perl mailing
list?
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/