Consider code:
char *word = NULL; // Pointer at buffered string.
int size = 0; // Size of buffered string.
int index = 0; // Write index.
char c; // Next character read from file.
FILE *file = fopen(fileDir, "r");
if (file)
{
while ((c = getc(file)) != EOF)
{
printf("Current index: %d, size: %d, Word: %s\n", index, size, word);
if (isValidChar(c))
{
appendChar(c, &word, &size, &index);
}
else if (word) // Any non-valid char is end of word. If (pointer) word is not null, we can process word.
{
// Processing parsed word.
size = 0; // Reset buffer size.
index = 0; // Reset buffer index.
free(word); // Free memory.
word = NULL; // Nullify word.
// Next word will be read
}
}
}
fclose(file);
/* Appends c to string, resizes string, inceremnts index. */
void appendChar(char c, char **string, int *size, int *index)
{
printf("CALL\n");
if (*size <= *index) // Resize buffer.
{
*size += 1; // Words are mostly 1-3 chars, that's why I use +1.
char *newString = realloc(*string, *size); // Reallocate memory.
printf("REALLOC\n");
if (!newString) // Out of memory?
{
printf("[ERROR] Failed to append character to buffered string.");
return;
}
*string = newString;
printf("ASSIGN\n");
}
*string[*index] = c;
printf("SET\n");
(*index)++;
printf("RET\n");
}
For Input:
BLOODY
Output:
Current index: 0, size: 0, Word: <null>
CALL
REALLOC
ASSIGN
SET
RET
Current index: 1, size: 1, Word: B** // Where * means "some random char" since I am NOT saving additional '\0'. I don't need to, I have my size/index.
CALL
REALLOC
ASSIGN
CRASH!!!
So basically - *string[*index] = 'B' works, when index is first letter, it crashes at second one. Why? I probably messed up allocation or pointers, I don't really know (novice) :C
Thank you!
EDIT I also ment to ask - is there anything else wrong with my code?
This expression is incorrect:
*string[*index] = c;
Since []
's precedence is higher than *
's, the code tries to interpret the double pointer string
as an array of pointers. When *index
is zero, you get the right address, so the first iteration works by pure coincidence.
You can fix this by forcing the right order of operations with parentheses:
(*string)[*index] = c;