2011-11-18 78 views
0

所以我试图在C中实现一个缓存。我已经包含了一个非常简单的我的代码版本。不兼容的指针类型错误C

我不断收到此错误:

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; 
} 

我真的想不通,我做错了什么......我的逻辑是,由于我在我的结构中作为一个指针,在我创建内存中的结构之后,我可以轻松地遍历随后的列表。我的逻辑错误在哪里?

编辑最初的问题是固定的(我忘了在我的类型声明下划线的结构node_未来;.

现在我有一个问题:当我尝试在底部通过列表步骤我的代码打印出包含在列表中的话,我基本上不能够逐步通过列表我保持输出:

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 

出于某种原因,似乎我的程序是不是我的存储在第一次迭代之后,我在内存中的列表发生了变化。关于我在做什么错误的任何想法?

再次,任何帮助将不胜感激,谢谢。

+1

请不要typedef指针看起来像非指针。杀死可读性。 – Kos

+0

这实际上是一个很好的提示,它真的没有发生在我身上。 你的意思是喜欢而不是typedef结构node_ *节点,我应该做一些像typedef结构node_ nodeptr? – gfppaste

+0

@gfppaste:参见Jens的答案。 –

回答

4

在您的结构定义中,您没有下划线struct node

你最好有一个向前声明

typedef struct node node; 

,然后宣布你的结构

struct node { 
... 
node *next; 
}; 

没有必要有这个下划线的东西,并在typedef隐藏*。这只会让你很容易混淆。

1

字符串文字"like this"const char*而不是char*,因为它们是不可变的。

修复您的声明const char*并且警告将消失。

+0

对于这种无聊的评论,我很遗憾,但是技术上字符串文字有一个char数组类型*,没有const *,但它是UB来修改它们。 'const char *'绝对是引用字符串文字的方式。 – u0b34a0f6ae

+0

嗯......我确定C99正式固定它,显然它没有!感谢你提到这一点。 – Kos

1

我认为struct member'next'必须声明为(node_ *)类型。正如现在写的(node_ **)