strcat messing things up?!

This is a discussion on strcat messing things up?! within the C Programming forums, part of the General Programming Boards category; I am having problems with this little program. It should output something like 33 p1, 29 p1, each one properly ...

strcat messing things up?!

I am having problems with this little program. It should output something like 33 p1, 29 p1, each one properly positioned inside a cell, but because of the line with strcat, ) it is getting messed up and I get the output as here:

You have a buffer overflow. nums isn't big enough. It's a 2-d array that is 10 rows long by 5 chars wide. You need to change 5 to something larger. You're not leaving enough space for the null terminator after you concatenate " p1" on to the end. Basically, you end up with 6 chars, '3', '3', ' ', 'p', '1' and '\0'. strcat add that null terminator, but there's no room, so it overflows into the next row of nums. When you scanf into the next row, you overwrite the null terminator for the previous row, so it appears the two strings are smashed together, and get the output you see.

You have a buffer overflow. nums isn't big enough. It's a 2-d array that is 10 rows long by 5 chars wide. You need to change 5 to something larger. You're not leaving enough space for the null terminator after you concatenate " p1" on to the end. Basically, you end up with 6 chars, '3', '3', ' ', 'p', '1' and '\0'. strcat add that null terminator, but there's no room, so it overflows into the next row of nums. When you scanf into the next row, you overwrite the null terminator for the previous row, so it appears the two strings are smashed together, and get the output you see.