1

だから私はオペレーティングシステムのコースを受講していて、Cプログラミングのクラッシュコースから学期を始めました。最初の2つの割り当ては簡単でしたが、私はこれを一生理解することができません。

そのため、教授はTAに次の.cファイルを作成させました。

#include <stdio.h>
#include <stdlib.h>
#include "dll.h"

void init(Dll *dll, int n){
Node *prev = &(dll->head);
prev->prev = NULL;
Node *node;
for(int i=0;i<n;i++){
    node = (Node *)malloc(sizeof(Node));
    node->value = rand();
    node->prev = prev;
    node->next = NULL;
    prev->next = node;
    prev = node;
    printf("%d. ", i+1);
    printf("%d\n", node->value);
}
}
void print(Dll *dll){
Node *current;
current = dll->head.next;
while(current->next != NULL){
    printf("%d\n", current->value);
    current = current->next;
}
printf("%d\n", current->value);
}
void sort(Dll *dll){
int changed = 1;
while(changed==1){
    Node *current;
    current = dll->head.next;
    changed = 0;
    while(current->next != NULL){
        if(current->value > current->next->value){
            int value;
            value = current->value;
            current->value = current->next->value;
            current->next->value = value;
            changed = 1;
        }
        current = current->next;
    }
}

}

int main(){
Dll dll;
init(&dll, 10);
print(&dll);
sort(&dll);
printf("\nSorted:\n");
print(&dll);
return 0;
}

そして、私たちの割り当ては、.cファイルを実行するために実装する必要があるヘッダーファイルを作成することです。私は一日中それに取り組んできました、そして私が思いつくことができる最高のものは次のとおりです:

#include <stdio.h>
#include <stdlib.h>

typedef struct Dll {
Dll *prev;
Dll *next;
int value;
} dll;


void *init(Dll *dll, int n);
void sort(Dll *dll);
void print(Dll *dll);

.hで実行すると、コマンドラインで次のエラーが発生します(Linuxではgccを使用しています)。

In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Dll’
dll.h:12:12: error: unknown type name ‘Dll’
dll.h:13:11: error: unknown type name ‘Dll’
dll.h:14:12: error: unknown type name ‘Dll’
dll.c:7:11: error: unknown type name ‘Dll’
dll.c:22:12: error: unknown type name ‘Dll’
dll.c:31:11: error: unknown type name ‘Dll’
dll.c: In function ‘main’:
dll.c:51:2: error: unknown type name ‘Dll’

これらのタイプのヘッダーファイルの他の例を使用して作成しましたが、何らかの理由でこれを理解できません。私はあなたたちが私に与えることができるどんな助けでも本当に感謝します。

*編集:*名前を「Dll」に変更すると、次のようになります。

In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Type’
dll.h:12:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
dll.c:7:6: error: conflicting types for ‘init’
dll.h:14:7: note: previous declaration of ‘init’ was here
dll.c: In function ‘init’:
dll.c:8:2: error: unknown type name ‘Node’
dll.c:8:20: error: ‘Dll’ has no member named ‘head’
dll.c:9:6: error: request for member ‘prev’ in something not a structure or union
dll.c:10:2: error: unknown type name ‘Node’
dll.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
dll.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code
dll.c:12:11: error: ‘Node’ undeclared (first use in this function)
dll.c:12:11: note: each undeclared identifier is reported only once for each function it         appears in
dll.c:12:17: error: expected expression before ‘)’ token
dll.c:13:7: error: request for member ‘value’ in something not a structure or union
dll.c:14:7: error: request for member ‘prev’ in something not a structure or union
dll.c:15:7: error: request for member ‘next’ in something not a structure or union
dll.c:16:7: error: request for member ‘next’ in something not a structure or union
dll.c:19:22: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘print’:
dll.c:23:2: error: unknown type name ‘Node’
dll.c:24:15: error: ‘Dll’ has no member named ‘head’
dll.c:25:15: error: request for member ‘next’ in something not a structure or union
dll.c:26:25: error: request for member ‘value’ in something not a structure or union
dll.c:27:20: error: request for member ‘next’ in something not a structure or union
dll.c:29:24: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘sort’:
dll.c:34:3: error: unknown type name ‘Node’
dll.c:35:16: error: ‘Dll’ has no member named ‘head’
dll.c:37:16: error: request for member ‘next’ in something not a structure or union
dll.c:38:14: error: request for member ‘value’ in something not a structure or union
dll.c:38:31: error: request for member ‘next’ in something not a structure or union
dll.c:40:20: error: request for member ‘value’ in something not a structure or union
dll.c:41:12: error: request for member ‘value’ in something not a structure or union
dll.c:41:29: error: request for member ‘next’ in something not a structure or union
dll.c:42:12: error: request for member ‘next’ in something not a structure or union
dll.c:45:21: error: request for member ‘next’ in something not a structure or union
4

2 に答える 2

1

それは次のようになります。

typedef struct _Node{
   struct _Node *next;
   struct _Node *prev;
   int value;
}Node;
typedef struct _Dll{
   Node head;
};

あなたのtypedef..のタイプミスdllはと同じではありませんDll

また、あなたの中にはありませんことに注意headしてくださいstruct _Dll

また、何であるか教えてくださいNode

于 2013-02-06T05:06:16.173 に答える
0

質問コードには、次の 2 つのタイプ名があります。

struct Dll

そして、そのためのtypedef

dll

C++ とは異なり、C ではキーワード struct が重要です。次に、コードで Dll のみを使用しますが、これはCコンパイラの既知のシンボルではありません。

これを行うことも問題ありません。別の名前を使用する必要はありません。

typedef struct my_struct { ... } my_struct;

queation コードのもう 1 つの問題は、パラメーター名 dll と typedef 名 dll があることです。

于 2013-02-06T05:50:40.317 に答える