So I've very nearly finished my final project (I think) but have come up against a problem that I think is very simple but I'm having trouble fixing.
In the following code I have a global string key, initialised via string key;
at the beginning of my program that I want to have no value at the start of the subroutine findkey. I then want to append the letter c to key to then be used in another subroutine. Currently I am getting a seg fault due to an invalid write of size 1 in the line key[i]=c;
.
The variable key is assigned elsewhere using GetString
but only if the program goes down a different path. But the variable key
, regardless of where it gets it's value, must feed into another subroutine as a string.
Any help would be hugely appreciated!
int findkey(const char* dictionary, int pos, string key)
{
// Open the dictionary file
FILE* filein = fopen(dictionary, "r");
key = NULL;
for (int c = fgetc(filein); c != EOF; c = fgetc(filein))
{
int i = 0;
// check whether the character is alphabetical or '
if (isalpha(c) || (c == '\''))
{
key[i] = c;
i++;
}