问题
I am creating a linked list by reading in a text file and inserting the letters in alphabetical order in the list. I need to print the list, but cannot seem to get the correct function. I keep getting an error
error: invalid type argument of ‘->’ (have ‘order_list’)
error: invalid type argument of ‘->’ (have ‘order_list’)
I know this is incorrect, but I am at a loss for correctly stating the print_alph function. Any help in finding a way to correctly print my list would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node_alph {
int key;
struct list_node_alph *rest_old;
} list_node_order;
typedef struct {
list_node_order *the_head;
int size;
} order_list;
list_node_order *rec_alph_order(list_node_order * old_list, int new_key);
void insert_node(order_list *the_alph, int key);
void print_alph(order_list my_list);
list_node_order *rec_alph_order(list_node_order *old_list, int new_key) {
list_node_order *new_list;
if (old_list == NULL) {
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = NULL;
} else if (old_list->key >= new_key) {
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = old_list;
} else {
new_list = old_list;
new_list->rest_old = rec_alph_order(old_list->rest_old, new_key$
}
return (new_list);
}
void insert_node(order_list * the_alph, int key) {
++(the_alph->size);
the_alph->the_head = rec_alph_order(the_alph->the_head, key);
}
void print_alph(order_list my_list) {
printf("Pangram in alphabetical order: ");
while(my_list->head != NULL) { //ERROR
printf("%c", my_list->the_head); //ERROR
}
}
int main(void) {
int ch_count;
int count_pangram;
char *pang_arr;
FILE *alph_text;
alph_text = fopen("pangram.txt", "r");
if (alph_text == NULL) {
printf("Empty file. \n");
}
order_list my_alph = {NULL, 0};
while (( ch_count = fgetc(alph_text)) != EOF) {
putchar(ch_count);
char next_key;
int the_count;
for (the_count = 0; the_count < 100; the_count++) {
if (fscanf(alph_text, "%c", &next_key) != ' ') {
//order_list my_alph = {NULL, 0};
//for(next_key; next_key != SENT; scanf("&c", &next_key$
insert_node(&my_alph, next_key);
}
}
}
print_alph(my_alph);
fclose(alph_text);
return(0);
}
回答1:
Here in print_alph() you are passing instance of type order_list
so to access its member you should use . not ->
so change
while(my_list->head != NULL){
to
while(my_list.the_head != NULL){
But i think instead of passing its instance you should pass pointer of that object in print_alph()
In that case -> is fine to access its member.
void print_alph(order_list *my_list)
and call it as
print_alph(&my_alph);
回答2:
You need to use . instead of -> inside print_alph function as you have not passed order_list as pointer
void print_alph(order_list my_list){
printf("Pangram in alphabetical order: ");
while(my_list.head != NULL){
printf("%c", my_list.the_head);
}
}
来源:https://stackoverflow.com/questions/23003948/c-program-print-linked-list-from-recursive-ordering-function