sizeof trouble

Hi everyone,

I made a little piece of code that generates a random name for my window, but I have some unexpected behaviour. This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
srand(time(NULL));
	char* s1, * s2, * s3;
	char* s1_options[] = {"a", "b", "c", "d", "e"};
	char* s2_options[] = {"1", "2", "3", "4", "5", "6"};
	char* s3_options[] = {"more", "options", "etc"};

	s1 = s1_options[rand() % (sizeof(s1_options) / sizeof(char*))];
	s2 = s2_options[rand() % (sizeof(s2_options) / sizeof(char*))];
	s3 = s3_options[rand() % (sizeof(s3_options) / sizeof(char*))];

	char b[201];
	sprintf(b, "%s: %s to the %s", s1, s2, s3);
	b[200] = 0;
        std::string result = std::string(b);


Usually, I get the results I expected: Output like "a: 1 to the more" or "d: 5 to the etc". However, sometimes I get stuff like:
"ab: 3 to the options".

How can this happen? Cheers!
How do you print the string? Are you sure the extra character comes from printing the string result?
Oh my, just found out what caused the problem:

I had missed a comma in one of my arrays:

{"a", "b", "c", "d" "e"};

I would have thought this would cause a compiler error but apparantly it's fine?

Sorry for wasting anyone's time :|

Cheers!
It's an old feature that C++ inherited from C.
Topic archived. No new replies allowed.