C++ Arrays

Started by
12 comments, last by sevak 20 years, 8 months ago
A couple issues with those examples (such as the possibility of jumping beyond the bounds of your character arrays, etc), but the main problem (I believe) is that you are not initializing your strings (in particular, your full name string).

When you first create a string in C, it could contain anything...garbage, data previously occupying that memory, etc. The strcat function will append to strings, but even though YOU know full_name is a string, you haven''t set it to an empty string - and that ''E'' you see, is the garbage.

Try using strcpy(full_name, "") prior to your strcat statements. That should fix it (haven''t got VC++ installed just yet so I can''t test it).

Besides that, I highly suggest you make sure on your strcat functions that you are not going beyond the bounds of your arrays (either by checking the length with strlen or using strncat to only concat a certain number of characters.

Hopefully that helped somewhat.

Cheers
The guy who invented the first wheel was an idiot. The guy who invented the other three, he was the genius.
Advertisement
For example:

strcpy(full_name, first_name);
strcat(full_name, " ");
strcat(full_name, last_name);

- BlueDev
[/quote]
Well I tried your program in dev-c++ and it works correctly even if you don''t initialize the char array, I''m not saying it''s the right way to do it but it works on that compiler. Just remember you should clear the string before using it or you''ll encounter problems like that...
The monkeys are listening...
Its all about pointers....
http://www.gamedev.net/reference/articles/article1697.asp

This topic is closed to new replies.

Advertisement