Итак, я пытаюсь реализовать кеш на C. I включили очень урезанную версию моего кода.
Я продолжаю получать эту ошибку:
prog.c: In function ‘addtolist’:
prog.c:29: warning: assignment from incompatible pointer type
prog.c:40: warning: assignment from incompatible pointer type
prog.c: In function ‘main’:
prog.c:72: warning: assignment from incompatible pointer type
из этого кода:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct node_
{
char * word;
int filenumber;
struct node * next;
};
typedef struct node_ * node;
node createnode()
{
node head;
head = malloc(sizeof(struct node_));
head->word = NULL;
head->next = NULL;
return head;
}
unsigned int addtolist(node head, char * word, unsigned int limit, int fileno)
{
unsigned int templimit = limit;
node temp;
node temphead = head;
while(temphead->next != NULL)
{
temphead = temphead->next;
}
temp = malloc(sizeof(struct node_));
temp->word =(char*) malloc(strlen(word)+ 1);
strcpy(temp->word, word);
temp->next = NULL;
temp->filenumber = fileno;
templimit = templimit - (strlen(word) + 1) - sizeof(struct node_)- sizeof(int);
printf("templimit is size %u\n", templimit);
if (templimit < limit && templimit > 0)
{
temphead->next = temp;
limit = limit - strlen(word) - 1 - sizeof(struct node_)- sizeof(int);
return limit;
}
else
{
free(temp->word);
free(temp);
return 0;
}
}
int main()
{
node newlist = createnode();
int i = 0;
unsigned int limit = 65;
unsigned int temp = limit;
while(temp > 0 && temp <= limit)
{
temp = addtolist(newlist, "Hello", temp, i);
i++;
printf("new limit is - \t%u\nfilenumber is - \t%d\n", temp,i);
}
node ptr = newlist;
while(ptr->next != NULL)
{
printf("node %d contains the word %s\n", ptr->filenumber, ptr->word);
ptr = ptr->next;
}
return 1;
}
Честно говоря, я не могу понять, что я делаю не так ... Моя логика заключалась в том, что, поскольку я определял тип своей структуры как указатель, после того, как я создам структуру в памяти, я смогу легко пройти по следующему списку. Где был изъян в моей логике?
EDIT первоначальная проблема была исправлена (я забыл подчеркивание в объявлении типа struct node_ next;
Теперь у меня возникает другая проблема: когда я пытаюсь пройтись по списку внизу моего кода, чтобы распечатать слова, содержащиеся в списке, я в основном не могу пройти по списку. Я продолжаю выводить:
templimit is size 43
new limit is - 43
filenumber is - 1
templimit is size 21
new limit is - 21
filenumber is - 2
templimit is size 4294967295
new limit is - 0
filenumber is - 3
node 0 contains the word (null)
node 0 contains the word Hello
По какой-то причине кажется, что моя программа не ' t сохранение моих изменений в моем списке в памяти после первой итерации. Есть идеи о том, что я делаю неправильно?
Еще раз приветствую любую помощь и спасибо.