peculiarities with char[] (what else is new?)

I feel like I'm spending a lot of time on here recently...

Anyway, I'm using a library that uses C strings, and only C strings. It's OMG SO kind of annoying. Most of the time, the system works fine, but in one of my functions, it's behaving very oddly.

I initialized a C string like so:
char letters[26];

Which normally creates a blank string full of '\0' symbols. But when I run printf("%s",letters), I get a whole bunch of gibberish returned to me. This has ramifications for the following function, because apparently none of the cells in "letters" are '\0':

1
2
3
4
5
6
7
for (int i=0;i<26;i++)
   {
   int position = (seed >> i) % 26; //seed is a previously generated uint32
   while (letters[position] != '\0')   //We never escape this loop;
      position = (position + 11) % 26; //none of the cells in letters are '\0'
   letters[position] = i+65;
   }

However, subsequent functions that use 'letters' act completely bonkers. So far, every other C string-using function in my program works properly; this is the only one that's acting funny.

Has anyone experienced problems like this before? I realize I haven't added much else of my code, and I apologize; most of it is built using this 3rd party library, so it wouldn't make much sense to anyone. Still, these are the only parts that actively modify the C string, and are of the most concern to me. Let me know if you need more information, I'll do my best to comply.
Last edited on
g0dwyn wrote
I initialized a C string like so:
char letters[26];

No that does not initialise the array to all zeros.

This does:
char letters[26] = {0};

This happens with all variables if you do not initialize them with some value. They are set to some random value (which can be very bad in the case of pointers), which is why you want to always initialize your variables.
Topic archived. No new replies allowed.