简体   繁体   English

引用调用的指针不起作用

[英]pointer called by reference doesnt work

Hi i have a problem with the next code which i had posted before here, but yesterday i was working on my computer and all of the sudden the screen froze and when i restarted it my code was gone!! 嗨,我对之前在此发布的下一个代码有疑问,但是昨天我在计算机上工作,屏幕突然冻结,当我重新启动它时,我的代码消失了! it was erased, so i had to start over. 它被删除了,所以我不得不重新开始。 The code had to search a codop in a linked list, but it doesnt work, i keep getting the message "CODOP NOT FOUND", i thought that the problem was in the calling of pointers as pass by value, so i called it by reference but it doesnt work either, if someone could tell me how to solve it i would apreciate it a lot 代码必须在链接列表中搜索一个codop,​​但是它不起作用,我不断收到消息“ CODOP NOT FOUND”,我认为问题出在通过值传递来调用指针,所以我通过引用来调用它但它也不起作用,如果有人可以告诉我如何解决它,我会很感激

#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);
}

The problem is with this line: 问题在于此行:

if(ptr->instruction == codop)

it compares address of strings not strings themselves. 它比较字符串的地址而不是字符串本身。 Use strcmp or something like that instead. 使用strcmp或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM