On Tue, 2005-11-22 at 13:26 +1100, ashley maher wrote:
> G'day,
>
> I know this is not even C 101 level but would some kind soul please
> explain to me why this is not even close to working.
>
> Regards,
>
> Ashley
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int somefunction(char *string1)
> {
> char *string2 = "some words\0";
>
> string1 = (char *)calloc(strlen(string2 + 1), sizeof (char));
>
> strcpy(string1, string2);
>
> return 0;
> }
>
>
> int main ()
> {
> char *string;
>
> somefunction(string);
>
> printf ("\n\nString is: %s\n\n", string);
>
> free (string);
>
> return 0;
> }
You are pasing a pointer to char to somefunction. Its not altering the
referenced memory area, rather its just allocating a new variable
locally.
int somefunction(char **string1location)
{
char * string1 = (char *)calloc(strlen(string2 + 1), sizeof (char));
*string1location = string1;
..
}
should work better.
Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.
signature.asc
Description: This is a digitally signed message part
-- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
