c# 函数的隐式声明//createString函数的问题

zhte4eai  于 2023-04-10  发布在  C#
关注(0)|答案(1)|浏览(171)

嘿,伙计们,我需要你的帮助.我学习我的C-考试在两天.为此,我写了一个简单的列表一点代码.我的问题是,我每次得到相同的错误:“函数'copyString'的隐式声明”。你能告诉我我犯了什么错误吗?
在德国,我们说:“亲爱的格鲁斯”

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

    struct listen{
        int id; 
        int wert; 

        char *namen; 
        char *strasse; 
        int plz;

        struct listen *next; 
    }; 

    typedef struct listen elem; 

    elem *createElem(int id, int wert){

        elem* newElem = (elem*)malloc(sizeof(elem)); 

        if(newElem == NULL) printf("War nischt!"); 

        newElem->id = id; 
        newElem->wert = wert; 
        newElem->namen = NULL; 
        newElem->strasse = NULL; 
        newElem->plz = 0;

        return newElem;  

    }

    int stringLen(char* len){
        int d=0; 

        while(*len++) d++; 

        return d; 
    }
    
    char *createStr(char* st)
    {
        char* newSt;
        
        newSt = (char*)malloc(stringLen(st)+1);

        if(newSt == NULL) {printf("Fehler");};

        copyString(st, newSt); 

        return newSt;  
    }
    
    void copyString(char *von, char *nach)
    {
        while(*von!='\0')
        {
            *nach=*von; 

            *von++; 
            *nach++; 
        }

        *nach = '\0'; 
    }
    
    elem* addToList(elem *list, int wert, char* namen, char* strasse, int plz)
    {

        elem* p = createElem(list->id, wert); 

        p->namen = createStr(namen); 
        p->strasse = createStr(strasse); 
        p->plz = plz; 

        list->id++;
        p->next = list->next; 
        list->next = p;  

        return list; 
    }

    elem* createList(){

        elem* liste = createElem(0, 0); 

        return liste; 
    }

    void PrintElem(Elem *E)
    {
    printf("ID: %d | Wert: %d | Name : %s | Strasse: %s | PLZ: %d", E->id, E->wert, E->namen, E->strasse, E->plz); 
    }

    void printList(elem* list){

        printf("Liste:"); 
        while(list->next != NULL)
        {
            printElem(list->next); 
            list = list->next; 
        }
    }

    int main(){

        elem* Liste01 = createList(); 

        Liste01 = addToList(Liste01, 4, "Peter", "Lustigweg", 15834); 

        printList(Liste01);

        return 0;  
    }

我试着重新排列声明的函数。

am46iovg

am46iovg1#

我尝试了你的代码,正如上面的好评论所指出的,“copyString”函数在“createStr”函数中使用之前需要移动。一旦我执行了这一点重构,我就能够编译程序并执行程序了。然而,查看代码时,我对它遍历列表结构有一些问题。很明显,这个链表中有两个元素;然而,只有第二个列表元素被打印。而且,似乎元素ID没有到达它想要的列表元素。这样,我在链表打印函数中添加了一些额外的代码,以便可以查看初始链表元素。下面是代码的重构版本。

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

typedef struct listen
{
    int id;
    int wert;

    char *namen;
    char *strasse;
    int plz;

    struct listen *next;
};

typedef struct listen elem;

void copyString(char *von, char *nach)
{
    while(*von!='\0')
    {
        *nach=*von;

        *von++;
        *nach++;
    }

    *nach = '\0';
}

elem *createElem(int id, int wert)
{
    elem* newElem = (elem*)malloc(sizeof(elem));

    if(newElem == NULL) printf("War nischt!\n");

    newElem->id = id;
    newElem->wert = wert;
    newElem->namen = NULL;
    newElem->strasse = NULL;
    newElem->plz = 0;

    return newElem;
}

int stringLen(char* len)
{
    int d=0;

    while(*len++) d++;

    return d;
}

char *createStr(char* st)
{
    char* newSt;

    newSt = (char*)malloc(stringLen(st)+1);

    if(newSt == NULL)
    {
        printf("Fehler\n");
    }

    copyString(st, newSt);

    return newSt;
}

void addToList(elem *list, int wert, char* namen, char* strasse, int plz)   /* FYI - made this a void function as the linked list pointer is already known and doesn't change */
{
    elem* p = createElem(list->id, wert);

    p->namen = createStr(namen);
    p->strasse = createStr(strasse);
    p->plz = plz;

    list->id++;
    p->next = list->next;
    list->next = p;

    return;
}

elem* createList()
{
    elem* liste = createElem(0, 0);

    return liste;
}

void printElem(elem *E)
{
    printf("ID: %d | Wert: %d | Name : %s | Strasse: %s | PLZ: %d\n", E->id, E->wert, E->namen, E->strasse, E->plz);
}

void printList(elem* list)
{
    printf("Liste:");
    printf("Data within initial list element\n");   /* Just to illustrate that the initial list element is not used */
    printElem(list);
    while(list->next != NULL)
    {
        printElem(list->next);
        list = list->next;
    }
}

int main()
{
    elem* Liste01 = createList();

    addToList(Liste01, 4, "Peter", "Lustigweg", 15834);

    printList(Liste01);

    return 0;
}

有了额外的打印信息,以下是终端的输出。

@Vera:~/C_Programs/Console/DeutschList/bin/Release$ ./DeutschList 
Liste:Data within initial list element
ID: 1 | Wert: 0 | Name : (null) | Strasse: (null) | PLZ: 0
ID: 0 | Wert: 4 | Name : Peter | Strasse: Lustigweg | PLZ: 15834

可以看出,当添加第二链表元素时,ID值被存储在初始链表元素内;然而,其他数据元素进入下一个链表元素。
我将留给您细化代码,以便初始链表元素包含初始数据,但是看看这是否能使您在项目的精神上向前推进。

相关问题