将*添加到用户定义的类型时不兼容的指针类型错误

时间:2017-12-20 05:31:25

标签: c debugging

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "textbuffer.h"

typedef struct textNode{ //basically contains a line + link
    char* line;
    int sLength; //length of string
    struct textNode* next; 
} tNode; 

struct textbuffer{
    int size; //number of lines in the text buffer
    tNode* head; 
    tNode* tail;
}

static tNode* newTN(char* string, int sLength, tNode* next);

static tNode* newTN(char* string, int sLength, tNode* next){
    tNode* t = malloc(sizeof(struct textNode));
    t->line = malloc(sizeof(char)*sLength);

    if(string != NULL){
        strcpy(t->line, string);
    } 
    t->next = next; 
    return t;
}

static void printBuffer(TB tb){
    tNode* curr = tb->head;
    int i = 0;
    while(curr != NULL){

        while(curr->line[i] != '\n'){
            printf("%c", curr->line[i]);
            i++;
        }
        printf("\n");
        i = 0;
        curr = curr->next;
    }
}

/* Allocate a new textbuffer whose contents is initialised with the text given
 * in the array.
 */
TB newTB (char text[]){

    assert(text != NULL); 

    int i = 0; 
    char c;

    TB tBuffer = malloc(sizeof(struct textbuffer)); 
    tBuffer->size = 0; 

    //tNode* currLine = malloc(sizeof (struct textNode*));
    tNode* currLine = newTN(NULL, 1, NULL);
    tNode* currtNode; 

    //currLine->line = malloc(sizeof(char));

在分配像&#34; curr = curr-&gt; next&#34;这样的内容时,我不断收到错误,我得到的第一个错误是:

期待&#39; =&#39;,&#39;,&#39;,&#39 ;;&#39;,&#39; asm&#39;或&#39; 属性&#39;在令牌之前&#39; *&#39;在我声明我的静态函数newTN的行中。请告诉我问题是什么......谢谢......太困惑了

1 个答案:

答案 0 :(得分:0)

typedef struct textNode的定义中,您已宣布struct textNode** next,即nextpointer-to-pointesdouble pointer。但是在功能static tNode* newTN(char* string, int sLength, tNode* next);中,您已将next声明为single-pointer

因此,在函数t->next = next;的行static tNode* newTN(char* string, int sLength, tNode* next);中,您无法将single-pointer分配给double pointer

要解决此问题,请在struct textNode** next中将typedef struct textNode声明为struct textNode* next,即single-pointer

<强>更新
struct textbuffer的定义不会以;终止。