通过引用调用的指针不起作用

时间:2014-01-27 19:02:27

标签: c

您好我在此之前发布的下一个代码有问题,但昨天我正在我的电脑上工作,突然屏幕冻结,当我重新启动它时,我的代码消失了!!它被删除了,所以我不得不重新开始。代码必须在链表中搜索一个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);
}

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

if(ptr->instruction == codop)

它比较字符串的地址而不是字符串本身。 请改用strcmp之类的东西。