私はCの初心者です。現在、Cでリンクリストを書いています。コンパイルすると、「互換性のないポインター型からの代入」について不平を言い続けます。私のコードは次のようなものです:
# include <stdio.h>
#include <stdlib.h>
struct snode{
struct snode *next;
int val;
};
struct slist{
struct snode *head;
struct snode *tail;
int len;
};
struct slist *mylist;
void slist_insert(int v){
struct snode *newnode;
newnode = (struct snode*)malloc(sizeof(struct snode));
newnode -> val = v;
newnode -> next = NULL;
if(mylist->head = NULL){
mylist->head = malloc (sizeof(struct snode));
mylist->tail = (struct snode*)malloc (sizeof(struct snode));
mylist->head = newnode;
mylist->tail = newnode;
};
else{
mylist -> tail -> next = (struct snode*)malloc (sizeof(struct snode));
mylist -> tail -> next = newnode;
};
mylist -> len +=1;
};
main(){
slist_insert(1);
slist_insert(2);
slist_insert(3);
slist_insert(4);
slist_insert(5);
struct snode *temp;
temp = (struct snode*)malloc(sizeof(struct snode));
temp = mylist-> head;
while(temp -> next != NULL){
printf("%d\n", temp -> val);
temp = temp -> next;
};
};
これが変更されたものです。Linuxターミナルを使用してこのプログラムを実行しています。私が使用しているコンパイラは gcc -std=gnu99 です
アップデート
slist.c: In function â:
slist.c:32: error: â without a previous â
slist.c: At top level:
slist.c:40: warning: return type defaults to â