Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链表计数器中的几个错误_C - Fatal编程技术网

C 链表计数器中的几个错误

C 链表计数器中的几个错误,c,C,所以我创建了一个链表计数器,这是我的程序 我想要什么: 我要它给我正确的计数 我得到的: 以下错误: linkedlist.c:12: warning: assignment from incompatible pointer type linkedlist.c:14: warning: assignment from incompatible pointer type linkedlist.c: In function ‘main’: linkedlist.c:22: warning: ass

所以我创建了一个链表计数器,这是我的程序

我想要什么: 我要它给我正确的计数

我得到的: 以下错误:

linkedlist.c:12: warning: assignment from incompatible pointer type
linkedlist.c:14: warning: assignment from incompatible pointer type
linkedlist.c: In function ‘main’:
linkedlist.c:22: warning: assignment from incompatible pointer type
linkedlist.c:23: warning: assignment from incompatible pointer type
linkedlist.c:24: warning: assignment from incompatible pointer type
linkedlist.c:25: warning: assignment from incompatible pointer type
我的代码 我意识到这不是实例化链表的好方法。不过这没关系,我强调的是我的计数器工作正常

我对我的守则作了一些修改。不,这不是一个C++程序,实际上是一个C程序。p> 我的程序现在可以运行了,但我想知道为什么我会收到这么多关于不兼容指针类型的警告。有什么想法吗?我相信这是一个简单的问题

 #include <stdio.h>
typedef struct {
  int x;
  char *y;
 struct CELL *next;
} CELL;

  int list_length(CELL *head){
    int counter;
    CELL *current;
    if(head->next!=NULL){
    current=head->next;
    for(counter=1;current!=NULL;++counter){
      current=current->next;}}
    else
      return 0;
    return counter;}


main(){
  CELL a,b,c,d,e;
  a.next=&b;
  b.next=&c;
  c.next=&d;
  d.next=&e;
  e.next=NULL;
  int l=list_length(&a);
  printf("The list length is %d \n",l);
}
#包括
类型定义结构{
int x;
char*y;
结构单元*下一步;
}细胞;
整数列表长度(单元格*表头){
整数计数器;
电池*电流;
如果(头部->下一步!=NULL){
当前=头部->下一步;
用于(计数器=1;当前!=NULL;++计数器){
当前=当前->下一步;}
其他的
返回0;
返回计数器;}
main(){
细胞a、b、c、d、e;
a、 下一步=&b;
b、 下一步=&c;
c、 next=&d;
d、 下一步=&e;
e、 next=NULL;
int l=列表长度(&a);
printf(“列表长度为%d\n”,l);
}

您没有包含
,并且在结构定义的末尾缺少分号。您的代码还有其他错误,比如返回指向局部变量的指针。在我看来,这是一个C++程序,但是你已经标记了C.< /P> < p>你没有包含<代码> <代码>,并且在结构定义的末尾缺少分号。您的代码还有其他错误,比如返回指向局部变量的指针。在我看来,这是一个C++程序,但是你已经标记了C.< /P> < P> C没有一个<代码>字符串< /C>数据类型…改用
char*

C没有
字符串
数据类型。。。请改用
char*

首先,要编译此程序

  • CELL不是类型,因此在类型声明中需要
    struct CELL
  • 结构的末尾需要分号
  • 使用
    char*
    代替
    string
    (string不是C数据类型)
  • 至于
    list_length()
    中的算法,您需要更像这样的算法:

    int list_length(struct CELL *head)
    {
        int counter = 0;
        while (head) {
            head = head->next;
            ++counter;
        }
        return counter;
    }
    
    typedef struct CELL_ {
        int x;
        char *y;
        struct CELL_ *next;
    } CELL;
    
    您还应该注意,
    instantiate()
    函数返回指向堆栈分配内存的指针,从而导致未定义的行为。将代码从
    instantiate()
    移到主函数中

    以下是一个完整的工作程序:

    #include <stdio.h>
    
    struct CELL {
        struct CELL *next;
    };
    
    int list_length(struct CELL *head)
    {
        int counter = 0;
        while (head) {
            head = head->next;
            ++counter;
        }
        return counter;
    }
    
    int main(void)
    {
        struct CELL a, b, c, d, e;
        a.next = &b;
        b.next = &c;
        c.next = &d;
        d.next = &e;
        e.next = NULL;
        printf("The list length is %d \n", list_length(&a));
        return 0;
    }
    

    首先,要编译这个程序

  • CELL不是类型,因此在类型声明中需要
    struct CELL
  • 结构的末尾需要分号
  • 使用
    char*
    代替
    string
    (string不是C数据类型)
  • 至于
    list_length()
    中的算法,您需要更像这样的算法:

    int list_length(struct CELL *head)
    {
        int counter = 0;
        while (head) {
            head = head->next;
            ++counter;
        }
        return counter;
    }
    
    typedef struct CELL_ {
        int x;
        char *y;
        struct CELL_ *next;
    } CELL;
    
    您还应该注意,
    instantiate()
    函数返回指向堆栈分配内存的指针,从而导致未定义的行为。将代码从
    instantiate()
    移到主函数中

    以下是一个完整的工作程序:

    #include <stdio.h>
    
    struct CELL {
        struct CELL *next;
    };
    
    int list_length(struct CELL *head)
    {
        int counter = 0;
        while (head) {
            head = head->next;
            ++counter;
        }
        return counter;
    }
    
    int main(void)
    {
        struct CELL a, b, c, d, e;
        a.next = &b;
        b.next = &c;
        c.next = &d;
        d.next = &e;
        e.next = NULL;
        printf("The list length is %d \n", list_length(&a));
        return 0;
    }
    

    将代码从
    instantiate()
    移动到
    main()
    是一种临时解决方案,近乎于黑客攻击。堆栈分配的链表节点对于(比如)嵌入式编程很有用,但是具有环境的通用C程序应该继续使用
    malloc
    …并且他还需要
    NULL
    他最后一个节点的
    next
    成员:
    e1->next=NULL将代码从
    instantiate
    移动到
    main
    使其成为有效的C程序。至于不使用malloc的决定,这将是特定于应用程序的决定。这里的预期应用似乎是试图理解链表是如何工作的。我认为这种方法在这种情况下是有效的。说到malloc chris lutz,你到底是如何使用它的?我应该熟悉malloc的功能,但后来我忘记了。有人能给我一个例子,如果我使用malloc,我的程序会是什么样子吗?除此之外,这篇文章非常有用。非常感谢,它使用了与malloc类似的
    calloc
    。要理解为什么要使用
    calloc
    malloc
    ,您需要了解两者之间的区别。将代码从
    instantiate()
    移动到
    main()
    是一种临时解决方案,几乎是一种黑客行为。堆栈分配的链表节点对于(比如)嵌入式编程很有用,但是具有环境的通用C程序应该继续使用
    malloc
    …并且他还需要
    NULL
    他最后一个节点的
    next
    成员:
    e1->next=NULL将代码从
    instantiate
    移动到
    main
    使其成为有效的C程序。至于不使用malloc的决定,这将是特定于应用程序的决定。这里的预期应用似乎是试图理解链表是如何工作的。我认为这种方法在这种情况下是有效的。说到malloc chris lutz,你到底是如何使用它的?我应该熟悉malloc的功能,但后来我忘记了。有人能给我一个例子,如果我使用malloc,我的程序会是什么样子吗?除此之外,这篇文章非常有用。非常感谢,它使用了与malloc类似的
    calloc
    。要理解为什么要使用
    calloc
    malloc
    ,您需要理解两者之间的区别。
    chary[]
    无效,除非它是
    struct
    的最后一个成员。您的结构定义仍然错误,需要一个结构标记。在C
    main
    中,还必须返回
    int
    main
    的参数列表应为
    void
    int argc,char**argv
    。空参数列表是C++声明,