I am trying to make my node size dynamically change with the size of the word from the dictionary in the load(dictionary) function.
I understand that the warning means I am trying to assign a pointer to an address to a char array, which is invalid. But how do I access the access the address which new_node->word
points to?
UPDATE I changed my code so that it compiles, the changes are in the code and screenshot below
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
int main(void)
{
FILE* speller = fopen("dictionary.txt", "r");
FILE* test = fopen("test.txt", "w");
while (feof(speller) == 0)
{
// malloc a node for each new word
node* new_node = malloc(sizeof(node));
fscanf(speller, "%s", new_node->word);
// resize the node to free the extra memory
char* temp = realloc(new_node->word, sizeof(*char)*(strlen(new_node->word)+1));
if (temp == NULL)
{
free(new_node);
printf("could not allocate memory\n");
break;
}
else
{
*new_node->word = *temp;
}
fwrite(&new_node->word, sizeof(new_node->word), 1, test);
My understanding of it is that new_node->word
points to the string
from fscanf
, and that realloc
returns a pointer to the new memory. new_node->word = "hello"
accesses the char[46]
array, not the address, so I understand why there is an error. But I don't understand how to update the pointer to the memory that stores new_node->word
, in the way new_node->next = head
assigns a pointer, instead of doing what I am doing and trying to assign an address to a char [46]
array.
UPDATE After making the reference to the char pointer *temp
equal the reference to the node *new_node->word
, I got the code to compile. However, looking through gdb, it still seems that new_node->word
is a char * array [46]
. Does that mean it did not realloc
?
It seems that having a dynamic array size in a struct
would be the main cause of the original problem. Is this because in order for a struct
to be effective its variables (ie word, node* next) must always be at the same offset in memory from the struct
? I'm asking this because I thought about working around by creating a node* next
linked to the index after NULL
in a char array[strlen(currentstring) + 2]
, but to find which index has the node* next
I'd have to check at each index number, correct? So in other words, this is a limitation of a hashtable
, with no solution?
UPDATE
I found part of the problem was with the fscanf
function, and that I need to use the m
modifier to get a char
string in my char *
pointer. I used the following code:
fscanf(speller, "%ms", &new_node->word);
to get the program to work with my hash function and return the correct index, however I still have an issue with a seg fault now, which I need to resolve. It happens near the end of the z's in my `load' function.
UPDATE
I've gotten the above code to work, the seg fault
due to another issue I posted here: Pset6 "Size" giving the wrong output
strlen(new_node->word)
returning the correct value in gdb, but it does not print correctly usingfwrite(&new_node->word, sizeof(strlen(new_node->word)), 1, test);
If someone could explain this as well in a chat, it's not as important but I'd like to know what's happeningcons *char
passed byspeller
tocheck
into a localchar array
so I could modify it for myhash function
, but I had to loop through each char and set it equal. Like you say, you can set*p = array
, but not the other way around. Is my problem mainly because I'm not usingstrings
correctly?