Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Can somebody please explain me, why this program is not working right?
I know it's about the arrays, malloc and strcpy.

C++
int main(int argc, char* argv[])

{

char string1[5] = "Test";
char string2[5];

string2[0] = 'T';
string2[1] = 'e';
string2[2] = 's';
string2[3] = 't';

printf("String 1: %s\n",string1);
printf("String 2: %s\n",string2);

return 0;

}

-> Shell:

Please help,...
Posted
Updated 12-Oct-13 15:37pm
v5

The question makes no sense, because you cannot ask why something is not working right without explaining what do you want to achieve and what should be expected as "right".

Nevertheless, looking at the code shows that string2 has undetermined last character. Most C function assumes that this character should be null. As null character is missing, the code of printf "does not know where to stop" and may print some garbage. This should answer your question.

Please see:
http://en.wikipedia.org/wiki/Null-terminated_string[^].

You need to add string2[4] to 0 (of course, not '0'!). But the practical initialization should be
C++
char* string2 = "Test";

In this case, the compiler adds a null character at the end, and specification of exact length if not needed. Well, all this stuff is a great lame and a real curse of programming, a source of all kinds of bugs and exploits, but this is how those C functions were historically created and are still used by now, with some minor replacements of some functions with some safer ones…

—SA
 
Share this answer
 
v4
In your code you have:
C++
char string2[5];        // reserve 5 bytes. Note that the content is indeterminate

string2[0] = 'T';       // set element zero to the character 'T'
string2[1] = 'e';       //
string2[2] = 's';       //
string2[3] = 't';       //
// Oops, do you see what's missing?

printf("String 2: %s\n",string2);

But you also need to terminate the string with string2[4] = '\0';.

You also seem to be a little confused between character variables and character array variables.
C++
char character = 'x'; // this is a single character variable, there is no null terminator
char string[] = "x";  // this is an array of two characters, x followed by a null

Note the use of single quotes for a character and double quotes for a string.
 
Share this answer
 
Comments
Danny Stark 13-Oct-13 6:23am    
Thank you sooo much Richard MacCutchan, that was what i was looking for.

Have all a nice day and be excellent !!
Thank you for answering my "not question". Promise I will give my very best for the next questions. :-)

A string "abcde" is a character vector (array of char), which ends with the null character. (The string is actually "abcde\0" and a string with only one letter would remain "c\0". '\0' is for termination of a string and will be stored for each string.) A string is a array of ASCII characters.
The computer processes not only numbers, but also strings. For achieving this purpose we use the data type char.

Here a easy example for the specified,...
C++
/* Character constant */
/* The variable char_variable saves a character constant 'x' */
 
char car_variable = 'x';
 
/* String */
/* In the array string_variable is the string "string\0" stored - 7 characters are needed as well because you also need to save '\0' to terminate. Contents of the array is thus string_variable "string\0" */
 
string_variable char [7] = "string";
 
/* You Note that here a string consisting of only one character is stored.
Contents of the array is string_variablex is "x\0" */
 
string_variablex char [10] = "x";


Thank you very much for your help @Sergey Alexandrovich Kryukov
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900