C 引用调用的指针不起作用
嗨,我对之前发布的下一个代码有问题,但是昨天我在电脑上工作,突然屏幕冻结了,当我重新启动时,我的代码消失了!!它被擦掉了,所以我不得不重新开始。代码必须在链表中搜索codop,但它不起作用,我一直收到消息“codop NOT FOUND”,我认为问题在于调用指针作为传递值,所以我通过引用调用它,但它也不起作用,如果有人能告诉我如何解决它,我将非常感激C 引用调用的指针不起作用,c,C,嗨,我对之前发布的下一个代码有问题,但是昨天我在电脑上工作,突然屏幕冻结了,当我重新启动时,我的代码消失了!!它被擦掉了,所以我不得不重新开始。代码必须在链表中搜索codop,但它不起作用,我一直收到消息“codop NOT FOUND”,我认为问题在于调用指针作为传递值,所以我通过引用调用它,但它也不起作用,如果有人能告诉我如何解决它,我将非常感激 #include <stdio.h> #include <stdlib.h> #include <string.h&
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char *instruction;
struct node *next;
}COD;
void printList(COD *head);
void SearchEndLine(FILE *hc12);
void listTABOP(COD **head);
COD *lastElement(COD **head);
COD *createNode(char *ins);
void insertEnd(char *ins,COD *last);
char *Operands_Table(FILE *hc12);
COD *searchCodop(COD *head,char *codop);
void Remove(char *c);
int main()
{
COD *head = NULL,*found = NULL;
char *codop = "BLE";
listTABOP(&head);
printList(head);
if((found = searchCodop(head,codop)) == NULL)
{
printf("CODOP NOT FOUND\n");
printf("%s\n",codop);
}
return 0;
}
void SearchEndLine(FILE *hc12)
{
int car;
while((car = fgetc(hc12))!= '\n')
;
}
void Remove(char *c)
{
char *ptr;
if(((ptr = strchr(c,'\n'))!= NULL)||((ptr = strchr(c,'\t'))!= NULL)||((ptr = strchr(c,' '))!= NULL))
*ptr = '\0';
}
void listTABOP(COD **head)
{
int car;
FILE *hc12;
COD *last = NULL;
char *ins;
if((hc12 = fopen("Tabla_OP.txt","r"))!= NULL)
{
while((car = fgetc(hc12))!= EOF)
{
if(car != '\t')
{
ins = Operands_Table(hc12);
if(*head == NULL)
*head = createNode(ins);
else
{
last = lastElement(head);
insertEnd(ins,last);
}
}
else
SearchEndLine(hc12);
}
}
else
printf("No se pudo abrir el archivo");
}
COD *lastElement(COD **head)
{
COD *ptr;
ptr = *head;
while(ptr->next != NULL)
ptr = ptr->next;
return ptr;
}
char *Operands_Table(FILE *hc12)
{
int car,lon = 0,pos;
char *c;
fseek(hc12,-1,SEEK_CUR);
pos = ftell(hc12);
do
{
car = fgetc(hc12);
lon++;
}while(car != '\t');
fseek(hc12,pos,SEEK_SET);
c = (char*)calloc((lon+1),sizeof(char));
fgets(c,lon+1,hc12);
Remove(c);
SearchEndLine(hc12);
return c;
}
COD *searchCodop(COD *head,char *codop)
{
COD *ptr;
for(ptr = head;ptr != NULL;ptr = ptr->next)
{
if(ptr->instruction == codop)
return ptr;
}
return NULL;
}
void insertEnd(char *ins,COD *last)
{
last->next = createNode(ins);
last->next->next = NULL;
last = last->next;
}
COD *createNode(char *ins)
{
int s;
COD *x;
x = (COD*)malloc(sizeof(COD));
s = strlen(ins);
x->instruction = (char*)malloc((s+1)*sizeof(char));
strcpy(x->instruction,ins);
x->next = NULL;
return x;
}
void printList(COD *head)
{
COD *ptr;
for(ptr = head;ptr != NULL;ptr = ptr->next)
printf("\n%s\n",ptr->instruction);
}
#包括
#包括
#包括
类型定义结构节点
{
字符*指令;
结构节点*下一步;
}鳕鱼;
无效打印列表(COD*头);
无效搜索结束行(文件*hc12);
无效列表选项卡(COD**水头);
化学需氧量*最后一个元素(化学需氧量**水头);
COD*createNode(char*ins);
无效插入(字符*插入,字符*最后);
字符*操作数\表(文件*hc12);
COD*searchCodop(COD*head,char*codop);
消除空洞(字符*c);
int main()
{
COD*head=NULL,*found=NULL;
char*codop=“BLE”;
列表选项卡(&head);
印刷品清单(标题);
if((find=searchCodop(head,codop))==NULL)
{
printf(“未找到CODOP\n”);
printf(“%s\n”,codop);
}
返回0;
}
无效搜索结束行(文件*hc12)
{
int轿车;
而((car=fgetc(hc12))!='\n')
;
}
无效删除(char*c)
{
char*ptr;
如果((ptr=strchr(c,,\n'))!=NULL);(ptr=strchr(c,,\t'))!=NULL);(ptr=strchr(c,,)!=NULL))
*ptr='\0';
}
无效列表选项卡(COD**head)
{
int轿车;
文件*hc12;
COD*last=NULL;
char*ins;
如果((hc12=fopen(“Tabla_OP.txt”,“r”)!=NULL)
{
而((car=fgetc(hc12))!=EOF)
{
如果(车!='\t')
{
ins=操作数\表(hc12);
如果(*head==NULL)
*头部=创建节点(ins);
其他的
{
最后一个=最后一个元素(头部);
插入(ins,最后一个);
}
}
其他的
搜索端线(hc12);
}
}
其他的
printf(“不存在任何问题”);
}
COD*最后一个要素(COD**水头)
{
COD*ptr;
ptr=*水头;
while(ptr->next!=NULL)
ptr=ptr->next;
返回ptr;
}
字符*操作数\表(文件*hc12)
{
车内,lon=0,位置;
char*c;
fseek(hc12,-1,SEEK_CUR);
pos=ftell(hc12);
做
{
car=fgetc(hc12);
lon++;
}while(car!='\t');
fseek(hc12,位置,寻道设置);
c=(char*)calloc((lon+1),sizeof(char));
fgets(c,lon+1,hc12);
删除(c);
搜索端线(hc12);
返回c;
}
COD*searchCodop(COD*head,char*codop)
{
COD*ptr;
对于(ptr=head;ptr!=NULL;ptr=ptr->next)
{
如果(ptr->指令==codop)
返回ptr;
}
返回NULL;
}
无效插入(字符*插入,字符*最后)
{
最后->下一步=创建节点(ins);
last->next->next=NULL;
最后一个=最后一个->下一个;
}
COD*createNode(char*ins)
{
int-s;
COD*x;
x=(COD*)malloc(sizeof(COD));
s=斯特伦(ins);
指令=(char*)malloc((s+1)*sizeof(char));
strcpy(x->指令,ins);
x->next=NULL;
返回x;
}
无效打印列表(COD*头)
{
COD*ptr;
对于(ptr=head;ptr!=NULL;ptr=ptr->next)
printf(“\n%s\n”,ptr->指令);
}
这一行有问题:
if(ptr->instruction == codop)
它比较字符串的地址,而不是字符串本身。
改用strcmp或类似的方法。我会花一些时间检查您的函数,特别是
操作数表
和searchEndOfLine
,后者如果没有'\n'
尾随,则保证永远旋转。此外,您的链表插入可能要简单得多,在本网站的答案中有许多很好的实现示例。