第六章-結構

問題:E:\code\test_c\main.c|20|error: conflicting types for 'strdup'|

            E:\code\test_c\main.c|69|error: conflicting types for 'strdup'|

不知道爲什麼會這樣?

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

#define MAXWORD 100

struct tnode {
    char *word;           /* 樹的節點 */
    int count;            /* 指向單詞的指針 */
    struct tnode *left;   /* 左子節點 */
    struct tnode *right;  /* 右子節點 */
};

struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);

void treeprint(struct tnode *);
int getword(char *, int);
char *strdup(char *);

/* 單詞出現頻率的統計 */
int main()
{
    struct tnode *root;
    char word[MAXWORD];

    root = NULL;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            root = addtree(root, word);
    treeprint(root);
    return 0;
}
/* addtree函數:在p的位置或p的下方增加一個w節點 */
struct tnode *addtree(struct tnode *p, char *w)
{
    int cond;
    if (p == NULL) {       /* 該節點爲空 */
        p = talloc();      /* 創建一個新節點 */
        p->word = strdup(w);
        p->count = 1;
        p->left = p->right = NULL;
    } else if ((cond = strcmp(w, p->word)) == 0)
        p->count++;        /* 新單詞與節點中的單詞匹配 */
    else if (cond < 0)     /* 如果小於該節點中的單詞,則進入左子樹 */
        p->left = addtree(p->left, w);
    else                   /* 如果大於該節點中的單詞,則進入右子樹 */
        p->right = addtree(p->right, w);
    return p;
};

/* treeprint函數:按序打印樹p */
void treeprint(struct tnode *p)
{
    if (p != NULL) {
        treeprint(p->left);
        printf("%4d %s\n", p->count, p->word);
        treeprint(p->right);
    }
}

/* talloc函數:創建一個tnode */
struct tnode *talloc(void)
{
    return (struct tnode *) malloc(sizeof(struct tnode));
};

char *strdup(char *s)
{
    char *p;

    p = (char *) malloc(sizeof(strlen(s)) + 1);
    if (p != NULL)
        strcmp(p, s);
    return p;
}


發佈了37 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章