0

I'm working on a assignment in Data structs with C and I was wondering what this code line meant? I'm creating a binary tree if that helps..

typedef struct {
char word[MaxWordSize+1];  <---- this one.
NodeData;

and if anyone has any suggestions for a site that helps explain code lines.. if there is anything like that, please let me know.

Thank you in advance!

I think it means the characters in the string has an additional char? I'm a noob in coding.. forgive me.

  • It's an array of characters, presumably to hold a NUL-terminated string of up to `MaxWordSize` characters plus the NUL terminator. Regarding how to "explain code lines", the best thing you can do is get yourself a good book on the C programming language and start reading it. That will introduce you to many fundamental concepts that are critical in being able to write, read and understand code. – paddy Mar 24 '23 at 02:38
  • C character arrays end with a byte of zeros to define the end of the string, this +1 is for that byte. – Ahmed AEK Mar 24 '23 at 02:41
  • Other responses are correct. It's a common bug to put 'n' characters into an array dimensioned to hold only 'n' characters (forgetting about a terminating '\0' required if the array is treated as a C string.) The `+1` shows the coder has accounted for this (or should have). The compiler will use the value of `MaxWordSize` (say 30), add 1, and dimension the array for (at least) 31 bytes. The sum of known values is done during compilation, not during execution. In this case, clarity is enhanced without any performance impacts. – Fe2O3 Mar 24 '23 at 02:52

0 Answers0