why

D

Darklight

I am using suse 9.3 pro
Why is it when i run this program in the konsole

i get the out put:
1st string: To be or not to be?
2nd string: To bTo be or not to be?

which it should not be

but when i run the same program in the ddd debugger i get
1st string: to be or not to be:
2nd string: to be

which it should be according to the book: c programming in easy steps
the program is below:

#include<stdio.h>
#include<string.h> /* make strncpy available */

int main(void)
{
char string1[] = "To be or not to be?";
char string2[6];

/* copy first 5 characters in string1 to string2 */
strncpy(string2, string1, 5);

printf("1st string: %s\n", string1);
printf("2nd string: %s\n", string2);
return 0;
}
 
R

Robert Gamble

Darklight said:
I am using suse 9.3 pro
Why is it when i run this program in the konsole

i get the out put:
1st string: To be or not to be?
2nd string: To bTo be or not to be?

which it should not be

but when i run the same program in the ddd debugger i get
1st string: to be or not to be:
2nd string: to be

which it should be according to the book: c programming in easy steps
the program is below:

#include<stdio.h>
#include<string.h> /* make strncpy available */

int main(void)
{
char string1[] = "To be or not to be?";
char string2[6];

/* copy first 5 characters in string1 to string2 */
strncpy(string2, string1, 5);

printf("1st string: %s\n", string1);
printf("2nd string: %s\n", string2);
return 0;
}

Despite its name, strncpy does not guarantee that the destination will
have a null terminator (and thus be a string). In your case, since
there is no null character in the first 5 characters of the source
string, 5 non-null characters will be stored in string2, the last
character is indeterminate. When you printf string2, undefined
behavior occurs because it is not a string. If you initialized string2
like this:
char string2[6] = {0}
you would get the behavior you expect.

Robert Gamble
 
S

Skarmander

Darklight said:
I am using suse 9.3 pro
Why is it when i run this program in the konsole

i get the out put:
1st string: To be or not to be?
2nd string: To bTo be or not to be?

which it should not be

but when i run the same program in the ddd debugger i get
1st string: to be or not to be:
2nd string: to be

which it should be according to the book: c programming in easy steps
the program is below:

#include<stdio.h>
#include<string.h> /* make strncpy available */

int main(void)
{
char string1[] = "To be or not to be?";
char string2[6];

/* copy first 5 characters in string1 to string2 */
strncpy(string2, string1, 5);
<snip>

Strings in C are null-terminated. strncpy copies only the first five
bytes of the string, which does not include a terminating null.
Therefore the string is not well-defined. You need either

string2[5] = '\0';

after the strncpy or

char string2[6] = {'\0'};

to initialize string2 with all NULs.

The reason you do not see this problem when debugging is because the
contents of uninitialized memory apparently include a NUL character, and
it happens to work. In general, anything could happen, including a
program crash.

S.
 
S

Skarmander

Skarmander said:
Darklight said:
I am using suse 9.3 pro
Why is it when i run this program in the konsole

i get the out put:
1st string: To be or not to be?
2nd string: To bTo be or not to be?

which it should not be

but when i run the same program in the ddd debugger i get
1st string: to be or not to be:
2nd string: to be

which it should be according to the book: c programming in easy steps
the program is below:

#include<stdio.h>
#include<string.h> /* make strncpy available */

int main(void)
{
char string1[] = "To be or not to be?";
char string2[6];

/* copy first 5 characters in string1 to string2 */
strncpy(string2, string1, 5);
<snip>

Strings in C are null-terminated. strncpy copies only the first five
bytes of the string, which does not include a terminating null.

Uh oh, overloading alert. Make this "NUL-terminated" and "terminating
NUL", to avoid confusion with null pointers and NULL.

S.
 
P

pete

Darklight said:
I am using suse 9.3 pro
Why is it when i run this program in the konsole

i get the out put:
1st string: To be or not to be?
2nd string: To bTo be or not to be?

which it should not be

but when i run the same program in the ddd debugger i get
1st string: to be or not to be:
2nd string: to be

which it should be according to the book: c programming in easy steps
the program is below:

Really? That's some bad code.
It doesn't put a null character in string2,
and there's no such thing a string without a null character, in C.

#include<stdio.h>
#include<string.h> /* make strncpy available */

int main(void)
{
char string1[] = "To be or not to be?";
char string2[6];

If it were

char string2[6] = "";

then that would be OK
/* copy first 5 characters in string1 to string2 */
strncpy(string2, string1, 5);


string2[5] = '\0';

/*
** If string2 is not initialized with null characters
** then you have to put one in there at the end.
*/
 
D

Darklight

Thanks for your help, but this is what it says in the book

The example below copies a string of five characters into an array
of six elements-so string2[5] receives a \0 null character.
 
S

Skarmander

Darklight said:
Thanks for your help, but this is what it says in the book

The example below copies a string of five characters into an array
of six elements-so string2[5] receives a \0 null character.
This goes to show you that you can't believe everything you read.

This is a pretty explicit mistake; I'd look around for another book.

According to the ACCU, this book is "Not Recommended".
http://www.accu.org/bookreviews/public/reviews/c/c003566.htm

S.
 
P

pete

Skarmander said:
Thanks for your help, but this is what it says in the book

The example below copies a string of five characters into an array
of six elements-so string2[5] receives a \0 null character..

I'm not a proponent of book burning per se,
but if you have fireplace and it gets cold this Winter ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top