我想创建一个程序来实现一个行编辑器,它将行作为输入并打印它们

我正在使用游标来操纵单链列表,但由于指针类型不兼容,在displayFromCursor,displayAll,moveCursor函数中出现错误,我在哪里出错了?
请帮忙,我现在停留在这里

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

typedef struct node
{
    char line[300];
    struct node *link;
}NODE;

typedef struct cursor
{
    NODE *prev;
    NODE *next;
}CURSOR;

typedef struct list
{
    NODE *link;
    int count;
}LIST;

void takeCommand(CURSOR **pCursor,LIST **pList);
void insertLine(CURSOR **pCursor,LIST**);
void deleteLine(CURSOR **pCursor,LIST **pList);
void moveCursor(CURSOR **pCursor,LIST **pList);
void displayFromCursor(CURSOR **pCursor);
void displayAll(LIST **pList);

void main(void)
{
    LIST *pList;
    pList->count = 0;
    pList->link = NULL;

    CURSOR *pCursor;
    pCursor->prev = NULL;
    pCursor->next = NULL;

    takeCommand(&pCursor,&pList);
    printf("BYE\n");
    return;
}

void takeCommand(CURSOR **pCursor,LIST **pList)
{
    char c;
    printf(">");
    c=getchar();
    switch(c)
    {
    case 'I': insertLine(pCursor,pList);
        case 'D': deleteLine(pCursor,pList);
            case 'M': moveCursor(pCursor,pList);
                case 'L': displayFromCursor(pCursor);
                    case 'A': displayAll(pList);
                        case 'X':return;
                        default : printf("Enter correct value again");
    }
    takeCommand(pCursor,pList);
}

void insertLine(CURSOR **pCursor,LIST **pList)
{
    char newLine[300];
    bool x=true;
    do
    {
        if(strcmp(newLine,"//"))
        {
            x=false;
        }
        else
        {
            scanf("%s",newLine);
            NODE *pNew;
            pNew = (NODE*)malloc(sizeof(NODE));
            strcpy(pNew->line,newLine);
            pNew->link = (*pCursor)->next;
            (*pCursor)->prev = pNew;
            ((*pList)->count)++;
        }
    }while(true);
    return;
}

void deleteLine(CURSOR **pCursor,LIST **pList)
{
    NODE *temp;
    temp->link = (*pCursor)->next;
    (*pCursor)->next = ((*pCursor)->next)->link;
    free(temp);
    ((*pList)->count)--;
    return;
}

void displayFromCursor(CURSOR **pCursor)
{
    if((*pCursor)->next !=NULL)
    {
        printf("%s\n",(*pCursor)->next->line);
        displayFromCursor( ((*pCursor)->next) );
    }
    return;
}

void displayAll(LIST **pList)
{
    if((*pList)->link !=NULL)
    {
        printf("%s\n",(*pList)->link->line);
        displayFromCursor( ((*pList)->link) );
    }
    return;
}

void moveCursor(CURSOR **pCursor,LIST **pList)
{
    int countNew=(*pList)->count;
    int temp,i;
    printf("Enter the number to move cursor\n");
    scanf("%d",&temp);
    countNew-=temp;
    while((*pCursor)->next !=NULL)
    {
        (*pCursor) = (*pCursor)->next;
        countNew--;
    }
    (*pCursor)->next = (*pList)->link;
    (*pCursor)->prev = NULL;
    for(i=0;i<countNew;i++)
    {
        (*pCursor)->prev = (*pCursor)->next;
        (*pCursor)->next = (*pCursor)->next->link;
    }
    return;
}

最佳答案

问题出在

displayFromCursor( ((*pCursor)->next) );

其中函数displayFromCursor期望struct CURSOR **作为参数,但实际上参数的类型为struct NODE *。与函数displayAll类似,只需将pCursor作为参数传递。

关于c - 调用函数期间出现不兼容的指针类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17298645/

10-12 15:01
查看更多