I have declared 3 pointers to char arrays (here is example code). Somehow, they all eventually end up with the same value. Function1 returns a char* with the value of "Hello".
char * char1;
char * char2;
char * char3;
strcpy(char3,"Goodby");
strcpy(char2,"Goodday");
strcpy(char1,Function1()); //char1="Hello"
printf(char1);
printf("
");
printf(char2);
printf("
");
printf(char3);
output looks like this:
Hello
Hello
Hello
I checked and somehow, char2 and char3 had gotten the same values as char1 on the strcpy statement that changed char1's value to "Hello". How could this happen? Some loss of a null character? The word "Hello" was retrieved from a text file by Function1, if that could have something to do with it. Thanks.
Comments
:
: char * char1;
: char * char2;
: char * char3;
[red][b]
char1 = malloc (32); // allocate 31 symbol for string #1
char2 = malloc (32); // allocate 31 symbol for string #2
char3 = malloc (32); // allocate 31 symbol for string #3
[/b][/red]
: strcpy(char3,"Goodby");
: strcpy(char2,"Goodday");
: strcpy(char1,Function1()); //char1="Hello"
[red]Here ^^^ when you copy data into a string it supposed to have room and char* without initialising it does not have any room. You have to allocate (and release after use) the memory for these strings.[/red]
: printf(char1);
: printf("
");
: printf(char2);
: printf("
");
: printf(char3);
[red][b]
// Release after using it:
free (char1);
free (char2);
free (char3);
[/b][/red]
:
: output looks like this:
: Hello
: Hello
: Hello
:
: I checked and somehow, char2 and char3 had gotten the same values as char1 on the strcpy statement that changed char1's value to "Hello". How could this happen? Some loss of a null character? The word "Hello" was retrieved from a text file by Function1, if that could have something to do with it. Thanks.
:
:
:
:
: :
: : char * char1;
: : char * char2;
: : char * char3;
: [red][b]
: char1 = malloc (32); // allocate 31 symbol for string #1
: char2 = malloc (32); // allocate 31 symbol for string #2
: char3 = malloc (32); // allocate 31 symbol for string #3
: [/b][/red]
:
: : strcpy(char3,"Goodby");
: : strcpy(char2,"Goodday");
: : strcpy(char1,Function1()); //char1="Hello"
: [red]Here ^^^ when you copy data into a string it supposed to have room and char* without initialising it does not have any room. You have to allocate (and release after use) the memory for these strings.[/red]
: : printf(char1);
: : printf("
");
: : printf(char2);
: : printf("
");
: : printf(char3);
:
: [red][b]
: // Release after using it:
: free (char1);
: free (char2);
: free (char3);
: [/b][/red]
:
: :
: : output looks like this:
: : Hello
: : Hello
: : Hello
: :
: : I checked and somehow, char2 and char3 had gotten the same values as char1 on the strcpy statement that changed char1's value to "Hello". How could this happen? Some loss of a null character? The word "Hello" was retrieved from a text file by Function1, if that could have something to do with it. Thanks.
: :
: :
: :
: :
: I tried this and it gave me the following error:
error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
:
: : :
: : : char * char1;
: : : char * char2;
: : : char * char3;
: : [red][b]
: : char1 = malloc (32); // allocate 31 symbol for string #1
: : char2 = malloc (32); // allocate 31 symbol for string #2
: : char3 = malloc (32); // allocate 31 symbol for string #3
: : [/b][/red]
: :
: : : strcpy(char3,"Goodby");
: : : strcpy(char2,"Goodday");
: : : strcpy(char1,Function1()); //char1="Hello"
: : [red]Here ^^^ when you copy data into a string it supposed to have room and char* without initialising it does not have any room. You have to allocate (and release after use) the memory for these strings.[/red]
: : : printf(char1);
: : : printf("
");
: : : printf(char2);
: : : printf("
");
: : : printf(char3);
: :
: : [red][b]
: : // Release after using it:
: : free (char1);
: : free (char2);
: : free (char3);
: : [/b][/red]
: :
: : :
: : : output looks like this:
: : : Hello
: : : Hello
: : : Hello
: : :
: : : I checked and somehow, char2 and char3 had gotten the same values as char1 on the strcpy statement that changed char1's value to "Hello". How could this happen? Some loss of a null character? The word "Hello" was retrieved from a text file by Function1, if that could have something to do with it. Thanks.
: : :
: : :
: : :
: : :
: : I tried this and it gave me the following error:
:
: error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
: Conversion from 'void*' to pointer to non-'void' requires an explicit cast
:
: :
malloc() returns a void * and some compilers don't allow you to assign a void * to another pointer variable directly so you will have to cast the void * that malloc returns to a char *. Just put [blue](char *)[/blue] in front all the malloc functions
e.g. char1 = (char *)malloc(32);
Everyone can be taught to sculpt: Michelangelo would have had to be taught how not to. So it is with the great programmers.
Alan J. Perlis