![]() You already do that simply by declaring a pointer. Such that it will ensure that the program will allocate as many bytes as are needed to store the pointer. You don't need any new nodes to look at nodes you already have, so you shouldn't be allocating any new nodes. No, you allocate memory when you need more memory. I was under the impression that I was supposed to allocate memory with malloc every time I declare a pointer Is it not necessary to allocate memory in this case because a node already has a place in memory? ![]() In my mind the program is working if i'm getting the correct output. Is this because memory is already allocated when I declare the node struct with these lines? I was able to produce the desired output upon your recommended changes, but I was also able to produce the result I was looking for regardless of which method I used. I was under the impression that I was supposed to allocate memory with malloc every time I declare a pointer, such that it will ensure that the program will allocate as many bytes as are needed to store the pointer. I understand that a pointer points to a spot of memory which can then be accessed later. I watch the lectures many times and often have questions about their content which is why I write my own code to play with the underlying concepts such that I know what the holes in my understanding are. : recipe for target 'hashtabletest' failed usr/include/stdlib.h:563:25: note: passing argument to parameter '_ptr' here Hashtabletest.c:198:10: error: passing 'node' (aka 'struct node') to parameter of incompatible type 'void *' Hashtabletest.c:176:19: error: member reference type 'node' (aka 'struct node') is not a pointer did you mean to use '.'? Hashtabletest.c:173:20: error: assigning to 'node' (aka 'struct node') from incompatible type 'struct node *' dereference with * Hashtabletest.c:173:28: error: member reference type 'node' (aka 'struct node') is not a pointer did you mean to use '.'? Hashtabletest.c:171:19: error: member reference type 'node' (aka 'struct node') is not a pointer did you mean to use '.'? Printf("cursor->word prints: %c\n", i, cursor->word) Hashtabletest.c:170:58: error: member reference type 'node' (aka 'struct node') is not a pointer did you mean to use '.'? Hashtabletest.c:167:27: error: member reference type 'node' (aka 'struct node') is not a pointer did you mean to use '.'?įor (int i = 0 cursor->next != NULL i++) If I then do as the compiler asks and instead and say When I try to do this the compiler tells meĮrror: initializing 'node' (aka 'struct node') with an expression of incompatible type 'node *' I tried to not use malloc by altering the lines that read Honestly, it seems like you might need to take a step back from hash tables and go over the lecture/shorts/notes for pointers and memory allocation first. cursor has a next member, which is probably what you meant to put here: cursor = cursor->next ![]() It doesn't have a next member for you to access. The issue the compiler complains about though is that you're trying to assign something that doesn't exist. Setting cursor->next would be modifying the current node itself, not modifying which node you're looking at. You want to change your cursor so that it is pointing at the next node in the list. You don't want to allocate any new nodes (at this point), you want to loop through your existing nodes, so no malloc here.Īs the compiler tells you, your issue is in your for loop, here: cursor->next = cursor->word->next įirst of all, you should be updating your cursor, not its next element. You use malloc when you want to allocate new memory. what does the error that says member reference base type 'char' is not a structure or union mean? How would I go about looping through my linked list and printing the strings in each node's word field? What am I doing wrong?įirst thing's first: You should not be using malloc for cursor. So it would seem that the compiler doesn't want me to point the cursor's next field of the node that the cursor is currently set to. Member reference base type 'char' is not a structure or union This code won't compile and gives me the following error Printf("cursor->word prints %s\n", cursor->word) I'm trying to loop through the entire list and print the strings stored in the word field of each node with the following lineįor (cursor = dog cursor->next != dog cursor->next = cursor->word->next) in my program I have a linked list of 3 nodes where each node's next pointer points to the next node, and the last node's next node points to the first node. It would seem that I'm using a looping method that won't compile and I was wondering why. I'm working on a program to teach me concepts behind the speller assignment.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |