6

I am trying to create an array of fixed-length "strings" in C, but have been having a little trouble. The problem I am having is that I am getting a segmentation fault.

Here is the objective of my program: I would like to set the array's strings by index using data read from a text file. Here is the gists of my current code (I apologize that I couldn't add my entire code, but it is quite lengthy, and would likely just cause confusion):

//"n" is set at run time, and 256 is the length I would like the individual strings to be
char (*stringArray[n])[256];
char currentString[256];

//"inputFile" is a pointer to a FILE object (a .txt file)
fread(&currentString, 256, 1, inputFile);
//I would like to set the string at index 0 to the data that was just read in from the inputFile
strcpy(stringArray[i], &currentString);
user3495690
  • 576
  • 3
  • 15
  • You declare `stringArray` as an array of `n` pointers to an array of 256 characters. That doesn't sound... "normal". – Some programmer dude Nov 07 '15 at 21:31
  • Maybe it is already answered here? [C Program: newbie confusion on working with character string arrays](http://stackoverflow.com/questions/10055946/c-program-newbie-confusion-on-working-with-character-string-arrays) – jayant Nov 07 '15 at 21:31
  • @JoachimPileborg Yes, I found that a little weird, as well.. That was based on the second answer here: http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – user3495690 Nov 07 '15 at 21:43

1 Answers1

5

Note that if your string can be 256 characters long, you need its container to be 257 bytes long, in order to add the final \0 null character.

typedef char FixedLengthString[257];
FixedLengthString stringArray[N];
FixedLengthString currentString;

The rest of the code should behave the same, although some casting might be necessary to please functions expecting char* or const char* instead of FixedLengthString (which can be considered a different type depending on compiler flags).

Cyan
  • 13,248
  • 8
  • 43
  • 78
  • Thanks, this looks like it makes sense. I'll test it out and get back to you! – user3495690 Nov 07 '15 at 21:33
  • Do you know if I will have to change the fread second parameter to 257 to accommodate the suggested change, as well? – user3495690 Nov 07 '15 at 21:37
  • No. The size of the container is different from the size of the file read. All you need to ensure is that the container is as big or larger than the file read. – Cyan Nov 07 '15 at 21:39
  • Okay, so it would still work if the container were the same size as the file read? – user3495690 Nov 07 '15 at 21:40
  • From an `fread()` perspective, yes. But if your input file contains no `\0` null terminating character, you will need to add one manually for `strcmp()` to work. If you don't know, I recommend to add it, just to be safe. Hence, read 256 bytes into a 257 characters containers, and make the last character equal to zero. Something like : `memset(currentString, 0, sizeof(currentString)); fread(&currentString, 256, 1, inputFile);` – Cyan Nov 07 '15 at 21:42
  • Thanks! This worked great, I only need to access the characters copied by index, so the null character wasn't necessary, but I'm sure if anyone else is having a similar problem, that suggestion will help them :) – user3495690 Nov 07 '15 at 21:48
  • This answer is incomplete. Just increasing the size to allow the null character to be stored does nothing unless you also explicitly use this possibility you've created to actually ensure null termination; that, or seeing them as true fixed length strings and not using any of the string functions and simply `memcpy`. Please update the answer for future readers. – MicroVirus Nov 07 '15 at 22:14
  • 2
    `char stringArray[n][256];` would work just as well, without introducing an array typedef (which obfuscates the code, IMHO). – M.M Nov 07 '15 at 22:18
  • @M.M I just realized that.. That's what I originally had, and I wound up changing it to try to fix a problem I was having (as it turns out, it wasn't necessary) – user3495690 Nov 07 '15 at 22:29