需要帮助在 C 中返回二维结构数组

问题描述 投票:0回答:1

我对 C 还很陌生,并不完全理解它。我想我需要为我想返回的二维数组分配内存,但我不确定如何分配内存,而且很困惑。这是我的全部代码,但问题应该出在我的 createInstructions 方法上。在我的 IDE 中,它显示“数组类型具有不完整的元素类型‘instructions[]’”和“将‘instructs’声明为多维数组必须对除第一个维度之外的所有维度都有边界”。我不完全理解这些意味着什么或如何修复它们。任何建议表示赞赏。

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

char getString(char fileName[]) {
    char string[1000];
    fopen(fileName, "r");
    fscanf("%s", string);
    return string;
}

typedef struct Tape {
    char value;
    struct Tape *next;
    struct Tape *previous;
} Tape;

typedef struct instructions {
    char writeState;
    char movement;
    char newState;
} instructions;

typedef struct data {
    char string[1000];
    char fileName[100];
    int numStates, startState, endState;
} data;


data* createData(char fileName[]) {

    data* d = (data*)malloc(sizeof(data));
    strcpy(d->fileName, fileName);
    FILE *file = fopen(fileName, "r");

    fscanf(file, "%s", d->string);
    fscanf(file, "%d", &d->numStates);
    fscanf(file, "%d", &d->startState);
    fscanf(file, "%d", &d->endState);

    fclose(file);
    return d;
}

void testData(data* d) {
    printf("String: %s\n", d->string);
    printf("Start State: %d\n", d->startState);
    printf("End State: %d\n", d->endState);
    printf("Number of States: %d\n", d->numStates);
    printf("File Name: %s\n", d->fileName); // Correctly print fileName
}

Tape* createTape(char c) {
    Tape* newTape = malloc(sizeof(Tape));
    newTape->value = c;
    newTape->next = NULL;
    newTape->previous = NULL;
    return newTape;
}

Tape* stringToTape(char string[]) {
    if (string[0] != 'A') {
        printf("Invalid input, tape must begin with A\n");
        return NULL;
    }
        Tape* head = createTape(string[0]);
        Tape* curr = head;
        for (int i = 1; i < strlen(string); i++) {
            curr->next = createTape(string[i]);
            curr->next->previous = curr;
            curr = curr->next;
        }
    return head;
}

instructions *createInstructions(data d) {
    char moveVal, writeState, readState, newState, currentState;
    FILE *file = fopen(d.fileName, "r");
    int numStates = d.numStates;
    char endState = d.endState;
    char cur = d.startState;

    instructions instructs[numStates - 1][127];

    while (cur != endState) {
        fscanf(file,"%d %d %d %d %d", &currentState,&readState,&writeState,&moveVal,&newState);
        instructs[currentState][readState].writeState = writeState;
        instructs[currentState][readState].movement = moveVal;
        instructs[currentState][readState].newState = newState;
        cur = newState;
    }

    fclose(file);
    return *instructs;
}

Tape* processTape(instructions instructs[][], Tape* head, data* d) {

    char endState = d->endState;
    char currentState = d->startState;
    char curVal = head->value;

    Tape* temp = head;

    while (currentState != endState) {
        temp->value = instructs[currentState][curVal].writeState;
        if (instructs[currentState][curVal].movement == 'R') {
            temp->next->previous = temp;
            temp = temp->next;
        }
        if (instructs[currentState][curVal].movement == 'L') {
            temp->previous->next = temp;
            temp = temp->previous;
        }
        currentState = instructs[currentState][curVal].newState;
        curVal = temp->value;
    }
    return head;
}

    void print_tape(Tape* head) {
        Tape *temp = head;
        while (temp->previous != NULL) {
            temp = temp->previous;
            while(temp!=NULL) {
                printf("%c", temp->value);
                temp = temp->next;
            }
        }
    }

int main() {
    return 0;
}
c multidimensional-array struct
1个回答
0
投票

在底层,C 中的数组实际上是一维的,编译器提供

[][]
语法是为了方便起见。

但是,如果您想分配一个具有一定维数(例如 2)的数组,但在编译期间没有一个维数是已知的,那么您必须回退到低级别,并声明该数组并使用它作为一维数组。

以下示例代码演示了这一点。

在此示例中,所有数组元素的类型均为

int

函数

address_of_2d_array_element()
返回数组中给定
x, y
坐标处的元素的地址。 数组的宽度和高度都被传递以用于错误检查。

函数

allocate_2d_array_of_int_and_fill_with_ascending_value()
顾名思义:它分配一个单维数组,表示具有给定宽度和高度的二维数组,并使用给定的升序值初始化数组中的每个单元格。

函数

main()
使用
42
作为升序值分配这样的数组,然后打印该数组。

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

int* address_of_2d_array_element( int* array, int w, int h, int x, int y )
{
    assert( x >= 0 && x < w );
    assert( y >= 0 && y < h );
    return &array[y * w + x];
}

int* allocate_2d_array_of_int_and_fill_with_ascending_value( int w, int h, int start_value )
{
    int* array = (int*)malloc( w * h * sizeof(int) );
    for( int y = 0;  y < h;  y++ )
        for( int x = 0;  x < w;  x++ )
            *address_of_2d_array_element( array, w, h, x , y ) = start_value++;
    return array;
}

int main(void)
{
    int w = 3;
    int h = 2;
    int* array = allocate_2d_array_of_int_and_fill_with_ascending_value( w, h, 42 );
    for( int y = 0;  y < h;  y++ )
    {
        for( int x = 0;  x < w;  x++ )
        {
            int value = *address_of_2d_array_element( array, w, h, x, y );
            printf( "%3d ", value );
        }
        printf( "\n" );
    }
    free( array );
}

上面的代码打印以下结果:

 42  43  44 
 45  46  47 

这里是编译器资源管理器:https://godbolt.org/z/bqM68d6G3

© www.soinside.com 2019 - 2024. All rights reserved.