#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'。请告诉我这是什么问题......谢谢...这么困惑
在typedef struct textNode
的定义中你宣布struct textNode** next
,即next
是pointer-to-pointes
或double 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
的定义不是以;
终止。