简体   繁体   English

指向无法访问二维数组值的结构的指针

[英]Pointer to a structure unable to access values of 2D array

I am currently in the process of writing a program that acts as a circuit.我目前正在编写一个充当电路的程序。 I have a gate structure that takes a 2D char array in order to hold variable names, yet when I try to access these variable names stored in the array outside of the while loop, the content is empty.我有一个门结构,它采用二维字符数组来保存变量名,但是当我尝试访问存储在 while 循环之外的数组中的这些变量名时,内容为空。

typedef struct Gate
{
    kind_t kind;
    int size;     // size of DECODER and MULTIPLEXER
    char **params; // length determined by kind and size (CHANGED FROM INT TO CHAR)
                  // includes inputs and outputs, indicated by variable numbers
} Gate;

typedef struct Node
{
    Gate *data;
    struct Node *next;
} Node;
// Linked list of gates & attributes
    while (fscanf(fp, "%16s", str) != EOF)
    {
        if (strcmp(str, "AND") == 0)
        {
            head = makeGate(fp, head, AND);
            length++;
        }
        else if (strcmp(str, "OR") == 0)
        {
            head = makeGate(fp, head, OR);
            length++;
        }
        else if (strcmp(str, "NAND") == 0)
        {
            head = makeGate(fp, head, NAND);
            length++;
        }
        else if (strcmp(str, "NOR") == 0)
        {
            head = makeGate(fp, head, NOR);
            length++;
        }
        else if (strcmp(str, "XOR") == 0)
        {
            head = makeGate(fp, head, XOR);
            length++;
        }
        else if (strcmp(str, "NOT") == 0)
        {
            //head = makeGate(fp, head, NOT);
            //length++;
        }
        else if (strcmp(str, "PASS") == 0)
        {
            //head = makeGate(fp, head, PASS);
            //length++;
        }
        else if (strcmp(str, "DECODER") == 0)
        {
            //
        }
        else if (strcmp(str, "MULTIPLEXER") == 0)
        {
            //
        }
        printf("%s\n", head->data->params[2]);
    }

    // plugs in values to circuit
    for (int i = 0; i < 3; i++)
    {
        printf("Stored string: %s\n", head->data->params[i]);
    }

` `

Node *makeGate(FILE *fp, Node *head, kind_t inGate)
{
    char str[17];
    Node *new_node = (Node *)malloc(sizeof(Node)); // Node of linkedlist that contains gate structure
    new_node->data = (Gate *)malloc(sizeof(Gate)); // Gate structure that keeps information about a gate
    new_node->next = head;

    new_node->data->kind = inGate;
    new_node->data->size = 3;
    new_node->data->params = malloc(3 * sizeof(char*));
    for (int i = 0; i < 3; i++)
    {
        new_node->data->params[i] = malloc(17 * sizeof(char));
    }

    fscanf(fp, "%16s", str);
    new_node->data->params[0] = str;
    fscanf(fp, "%16s", str);
    new_node->data->params[1] = str;
    fscanf(fp, "%16s", str);
    new_node->data->params[2] = str;
    return new_node;
}

` The printf statement inside the while loop works perfectly fine and is there purely for testing, however the for loop that prints each value of the array is different and prints nothing. ` while 循环内的 printf 语句工作得很好,纯粹是为了测试,但是打印数组每个值的 for 循环是不同的,什么也不打印。

I tried to fix this multiple times to no avail, I originally found this problem as I noticed that I had gotten memory leak, and when I freed where the memory leak should be, it throws that I am freeing a address that is not malloced.我尝试多次修复此问题但无济于事,我最初发现此问题是因为我注意到我有 memory 泄漏,当我释放 memory 泄漏应该在的位置时,它抛出我正在释放一个未分配的地址。

My only thought is I am somehow losing/skipping a node, but I am out of ideas我唯一的想法是我不知何故丢失/跳过了一个节点,但我没有想法

The following does not copy data from str into the struct ( you'd need strcpy ):以下不会数据str复制到struct中(您需要strcpy ):

new_node->data->params[0] = str;

What it does is copy the address of str into each element.它所做的是将str地址复制到每个元素中。 They all point to the same buffer/string.它们都指向相同的缓冲区/字符串。 And, str goes out of scope when the function returns.并且,当 function 返回时, str从 scope 中消失。

You can [and should ] just scan into the struct directly.可以[并且应该] 直接扫描到结构中。

So, change:所以,改变:

fscanf(fp, "%16s", str);
new_node->data->params[0] = str;

fscanf(fp, "%16s", str);
new_node->data->params[1] = str;

fscanf(fp, "%16s", str);
new_node->data->params[2] = str;

Into:进入:

fscanf(fp,"%16s",new_node->data->params[0]);
fscanf(fp,"%16s",new_node->data->params[1]);
fscanf(fp,"%16s",new_node->data->params[2]);

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

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