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

问题描述 投票:-1回答:1
#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));

在分配诸如“curr = curr-> next”之类的东西时,我不断收到错误,我得到的第一个错误是:

在声明我的静态函数newTN的行中,在标记'*'之前预期'=',',',';','asm'或'attribute'。请告诉我这是什么问题......谢谢...这么困惑

c debugging
1个回答
0
投票

typedef struct textNode的定义中你宣布struct textNode** next,即nextpointer-to-pointesdouble pointer。但在功能static tNode* newTN(char* string, int sLength, tNode* next);你宣布nextsingle-pointer

所以,在函数t->next = next;static tNode* newTN(char* string, int sLength, tNode* next);行中,你不能将single-pointer分配给double pointer

为了解决这个问题,在struct textNode** next宣布typedef struct textNodestruct textNode* next,即single-pointer

更新: struct textbuffer的定义不是以;终止。

© www.soinside.com 2019 - 2024. All rights reserved.