I have written a program which uses lists of structures. I am attempting to re-write with the functions broken down further, and am getting confused with pointers.
Here is some code, the basic purpose of which is to append a node onto a list. It should be passed the address of the current ultimate node in the list, and return the address of the new node once it's been appended. This new node would then be passed as the new ultimate node in the next call to the function.
Code:
struct node{
char string[8];
int indicator;
struct node *next_node;
};
calling_func()
{
struct node *head_node = NULL;
struct node *new_node = NULL;
...
new_node = append( *head_node, "string" );
...
}
struct node *
append( struct node **node,
char *word )
{
struct node *new_node = NULL;
new_node = (struct node *) malloc( sizeof( struct node ));
new_node->next_node = NULL;
new_node->indicator = 0;
strcpy( new_node->string, word );
if( *node == NULL )
*node = new_node;
else
*node->next_node = new_node; /*Compiler errors this line*/
return( &new_node );
}
I get two errors when compiling:
1. warning: left operand of "->" must be pointer to struct/union
2.assignment type mismatch:
struct node {array[8] of char string, int indicator, pointer to struct node {..} next_node} "="
pointer to struct node {array[8] of char string,
int indicator, pointer to struct node {..} next_node}
I don't understand why the compiler accepts the *node = new_node statement, but not the *node->next_node = new_node statement.
I've got confused in the if/else block of the append function, and I can't figure out what *node, or **node, or node or &node represent when they are in this function.
Could someone please explain?