gpt4 book ai didi

c - 调用函数时出现不兼容指针类型错误

转载 作者:行者123 更新时间:2023-11-30 17:50:24 27 4
gpt4 key购买 nike

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

我正在使用游标来操作单向链接列表,但由于指针类型不兼容,我在 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)->下一个));

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com