I am just starting out on pset5 reviewing the speller.c code. Looks pretty straightforward however I don't entirely understand the following variable declaration:

char word[LENGTH + 1];

So I understand it is creating an array of chars called word of size LENGTH + 1.

My question is, where does LENGTH come from? I don't think LENGTH has been defined elsewhere in the code and it doesn't seem to be a reserved word either.

What it it doing and how?

Thanks, GRN


LENGTH is the maximum word length allowed. Have you looked in dictionary.h? I don't remember if it was already defined there in the downloaded version, but if it isn't already, you need to do so.

If this answers your question, please click the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks cliff B. It is defined in dictionary.h like you said and this is mentioned later on in the problem spec too. My bad :) – GRN Nov 12 '15 at 6:01
  • No problem. It's a good lesson on where to look for something, and a reminder that code is often spread out across multiple files. – Cliff B Nov 12 '15 at 6:22
  • But why the debugger show it with no value ?? 'NO VALUE' – Nour Dec 19 '16 at 11:06
  • Why i it of size LENGTH + 1 instead of being of zice LENGTH - 1, because arrays are zero-indexed? LENGTH is defined as a global variable equal to 45. Why would you need to create an array of 46 elements to hold a word? – s.brody Jun 11 '17 at 17:55
  • When declaring an array, you need the number of elements in the array. Since the longest word is 45 chars, the array has to have at least 45 elements, numbered 0 to 44. Now, you're dealing with a string. That means you also need to deal with the end of string marker, \0. That accounts for the +1. So, 45 +1 = LENGTH +1 = 46. – Cliff B Jun 11 '17 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .