Где я могу поместить нулевой символ в свою функцию?
Я переношу связанный список символов в массив символов, и когда я распечатываю массив символов, я получаю вместе со строкой мусорное значение int как таковое:
2
this0
ис1
В6
userfile1
А1
all0
ОФ1
the3
words1
аре7
seperated0
by-1
А8
пробел
Я не уверен, где я должен поместить нулевую символьную строку в своем коде.
Что я уже пробовал:
fgets(str2, user_file_length, user_file);/*need to assign str2[user_file_length]*/ token = strtok(str2, " "); while(token != NULL) { /*printf( " %s\n", token );*/ user_list->head = insert_word(user_list, token); token = strtok(NULL, " "); } printf("\nThe userfile linked list is:\n "); display(user_list); printf("\n\n"); num_user_words = count(user_list); /*code to put userfile linked list into dynamically allocated array*/ user_array = (char**)malloc(num_user_words*sizeof(char*)); for(i=0;i<num_user_words;i++) { put_in_array(user_list, i, user_array); printf("\n %s", user_array[i]); }
Node* insert_word(Linked_List* list, void* word) { Node* new_node; Node* temp; char* ptr = malloc(sizeof(word));/*need to free*/ strcpy(ptr, word); new_node = (Node*)malloc(sizeof(Node));/*need to free*/ new_node->data = ptr; new_node->next = NULL; if(list->head == NULL) { list->head = new_node; } else { temp = list->head; while(temp->next != NULL) { temp = temp->next; } temp->next = new_node; } return(list->head); }
void put_in_array(Linked_List* list, int element, char** array) { int length; int count = 0; Node* temp; temp = list->head; while(temp != NULL) { if(count == element) { length = strlen((char*)(temp->data))-2; printf("%d", length); array[count] = (char*)malloc(length*sizeof(char*)); array[count] = (char*)(temp->data); } count++; temp = temp->next; } }