Should I always declare char array bigger than my string is?

  • A+
Category:Languages

So I know that you should declare char arrays to be one element bigger than the word you want to put there because of the /0 that has to be at the end, but what about char arrays that I don't want to use as words?

I'm currently writing a program in which i store an array of keyboard letters that have some function assigned to them. Should I still end this array with /0?

 


That is probably not necessary.

A null terminator is not a requirement for arrays of char; it is a requirement for "C-strings", things that you intend to use as unitary blobs of data, particularly if you intend to pass them to C API functions. It's the conventional way that the "length" of the string is determined.

But if you just want a collection of chars to use independently then knock yourself out.

We cannot see your code, but it sounds to me like you don't want or need it in this case.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: