gpt4 book ai didi

C编程strcat.asm not found错误调试

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:41 28 4
gpt4 key购买 nike

我正在做家庭作业,一直在尝试调试我的程序,但无济于事。我试图读入一个输入文件,然后将每个字符添加到链接列表的末尾,但我的程序一直崩溃,并说找不到 strcat.asm,以及“0x0FA74127 (msvcr120d.dll) 中未处理的异常...... . writing location 0x00000000000000”,从这里查看其他问题来看,这似乎是 NULL 指针的问题。在处理这个单一问题 3 小时后,我向大家寻求帮助,谢谢。这是完整的代码:

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


#define LENGTH 80

struct boggleDataNode {
char data[3];
struct boggleDataNode *nextData;
};

void readData(struct boggleDataNode **);
struct boggleDataNode* makeNode(char *);
void display(struct boggleDataNode *);

int main() {
struct boggleDataNode * head = NULL;
readData(&head);
display(head);
}

void readData(struct boggleDataNode **headRef){
//initializing variables including
//opening the input file
char dataLine[LENGTH];
char *data;
char *fileName = "BoggleData.txt";

FILE *filePointer;

printf("Trying to open file %s\n", fileName);
filePointer = fopen(fileName, "r"); //read mode
printf("Successfully opened file %s\n", fileName);

if (filePointer == NULL){
perror("Error while opening file. \n");
exit(0);
}

//loop until find end of file
while (fgets(dataLine, LENGTH, filePointer) != NULL){
for (char *tok = strtok(dataLine, " "); tok; tok = strtok(NULL, " "))
{
data = tok;
printf("data = %s\n", tok);
if (*headRef == NULL){
*headRef = makeNode(data);
head = *headRef;
}
else{
head->nextData = makeNode(data);
head = head->nextData;
}
}
}

fclose(filePointer);

}//close readData

struct boggleDataNode* makeNode(char *data){
struct boggleDataNode* temp = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
strcpy(temp->data, data);
temp->nextData = NULL;
return temp;

/*
//allocate node
struct boggleDataNode* temp = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
struct boggleDataNode* right = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));

if ((*headRef) == NULL){
strcpy((*headRef)->data, data);
(*headRef)->nextData = NULL;
}
else{
strcpy(temp->data, data);
temp->nextData = NULL;

// ======== The program appears to break here ================
right = (*headRef);
while (right->nextData != NULL)
right = right->nextData;

right->nextData = temp;
right = temp;
right->nextData = NULL;
}
} */
}

void display(struct boggleDataNode *headRef){

struct boggleDataNode *helper = headRef;

if (helper == NULL){
return;
}
else{
printf("============================================\n");
printf("Node # Data\n");
printf("============================================\n");

int counter = 1;

while (helper != NULL){
printf("%5d \t %2s\n", counter++, helper->data);
helper = helper->nextData;
}
}
printf("\n");
}

这是输入文件

D R L X E I
C P O H S A
N H N L Z R
W T O O T A
I O S S E T
N W E G H E
B O O J A B
U I E N E S
P S A F K F
I U N H M Qu
Y R D V E L
V E H W H R
I O T M U C
T Y E L T R
S T I T Y D
A G A E E N

最佳答案

让我们在 C 中尝试更面向对象的方法。

  1. 将 main 的正文更改为:

    struct boggleDataNode * head = NULL;
    readData(&head);
    display(head);

    NULL 很好地表示一个空链表。

  2. readData 定义的开头更正为(注意两个星号):

    void readData(struct boggleDataNode **headRef){

    这需要与您之前在程序中声明的原型(prototype)相匹配。

  3. addEntry 的声明/原型(prototype)替换为

    struct boggleDataNode* makeNode(char *);
  4. 并将addEntry的定义替换为

    struct boggleDataNode* makeNode(char *data){
    struct boggleDataNode* temp =
    (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
    strcpy(temp->data, data);
    temp->nextData = NULL;
    return temp;
    }

    请注意,这种方法更容易推理。它将分配内存,复制字符串数据,并留下指向下一个节点的链接,表明它没有指向任何东西。这基本上是后 C 语言中构造函数的工作。请注意,您在这里分配了内存,但稍后您需要释放该内存。

  5. 最后,将 readData 方法中的 addEntry 调用替换为:

    if (*headRef == NULL) {
    *headRef = makeNode(data);
    head = *headRef;
    } else {
    head->nextData = makeNode(data);
    head = head->nextData;
    }

    此外,您需要在 readData 方法中声明 struct boggleDataNode * head = NULL;

关于C编程strcat.asm not found错误调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064324/

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