如何在函数C中使用指向二维结构数组的指针

7z5jn7bk  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(158)

下面是我的代码:

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

#define X 5       
#define Y 7 

struct mystruct {
    char name[10];
    int id[1];
    int status[1];
};

void show_struct(struct mystruct* table, int X_selected, int Y_selected);

void main(void)
{
    struct mystruct* table[X][Y];

    int X_selected = 3;
    int Y_selected = 4;

    table[X_selected][Y_selected] = (struct mystruct*)malloc(sizeof(struct mystruct));

    strcpy(table[X_selected][Y_selected]->name, "NAME");
    *table[X_selected][Y_selected]->id = 0;
    *table[X_selected][Y_selected]->status = 1;

    show_struct(table, X_selected, Y_selected);
}

void show_struct(struct mystruct* table, int X_selected, int Y_selected)
{
    if (**CONDITION**) {
        printf("OK , STATUS IS 1");
    }
    else {
        printf("ERROR , STATUS IS NOT 1");
    }
}

我需要帮助来查找代码中的CONDITION,以检查状态是否为1
当i调试到第 * 行时 *show_struct(table,X_selected,Y_selected);**在开始工作之前,我可以看到状态=1已成功置于表中:
Memory Table

iszxjhcz

iszxjhcz1#

这里有一个可能比你要求的要多一点的解决方案。我为结构的数组创建了一个新的结构,它携带了大小信息,还有一些函数在这个“数组结构”上操作,而不是在原始指针上操作。
我的解决方案假设您最终希望在运行时输入数组大小(您的原型已经定义了数组大小;在这种情况下,您可以简单地定义在编译时已知大小的适当数组)。
“数组结构”myTable只包含一个指向实际数据的指针,这使得它非常小,可以通过值来传递。这种复制是肤浅的:副本指向与原始数据相同的数据。(这样我们就可以用一个函数来填充一个表,该函数通过值来获取数据,如fill_tbl(struct myTable tbl)
该程序有一个包含结构定义和函数声明的头文件myStructArray.h、一个包含函数实现的实现文件myStructArray.c和一个main.c文件。

我的结构数组. h

#ifndef MY_STRUCT_ARRAY_H
#define MY_STRUCT_ARRAY_H

#include <stdint.h> // size_t

struct mystruct {
    char name[10];
    int id;
    int status;
};

struct myTable
{
    size_t mXSz;
    size_t mYSz;

    struct mystruct* mStructs;
};

/// <summary>
/// return a table with a pointer to the dynamically allocated array 
/// </summary>
struct myTable createTable(size_t szX, size_t szY);

/// <summary>
/// get a pointer to an element in the given table
/// </summary>
struct mystruct* getStructPtr(struct myTable tbl, size_t x, size_t y);

/// <summary>
/// print  single struct
/// </summary>
/// <param name="s"></param>
void show_struct(struct mystruct* s);
/// <summary>
/// print the entire table
/// </summary>
/// <param name="tbl"></param>
void show_table(struct myTable tbl);

void destroy_table(struct myTable tbl);

#endif

我的结构数组. c

#include <stdint.h> // size_t
#include <stdio.h> // printf
#include <string.h> // memset
#include <stdlib.h> // malloc
#include "myStructArray.h"
#include <assert.h>

void show_struct(struct mystruct *s)
{
    if (s->id) {
        printf("OK, STATUS IS 1\n");
        printf("name: ->%s<-\n", s->name? s->name : "Null");
        printf("status: %i\n", s->status);
    }
    else {
        printf("ERROR, STATUS IS NOT 1");
    }
}

struct mystruct* getStructPtr(struct myTable tbl, size_t x, size_t y)
{
    assert(x < tbl.mXSz&& y < tbl.mYSz);
    return &tbl.mStructs[y * tbl.mXSz + x];
}

void show_table(struct myTable tbl)
{
    for (size_t y = 0; y < tbl.mYSz; y++)
    {
        for (size_t x = 0; x < tbl.mXSz; x++)
        {
            printf("*************** x: %zu, y: %zu******\n", x, y);
            show_struct(getStructPtr(tbl, x, y));
        }
        printf("\n");
    }
}

struct myTable createTable(size_t szX, size_t szY)
{
    struct myTable newTbl = {szX, szY, 0};
    
    size_t byteSz = szX * szY * sizeof(struct mystruct);
    newTbl.mStructs = (struct mystruct *) malloc(byteSz);
    memset(newTbl.mStructs, 0, byteSz); // make sure all ids are 0
    return newTbl; // yes, by value (two numbers and a pointer)
}

void destroy_table(struct myTable tbl)
{
    free(tbl.mStructs); // ok if null
}

主文件. c

#define _CRT_SECURE_NO_WARNINGS // for visual C

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

void fill_tbl(struct myTable tbl)
{
    for (size_t y = 0; y < tbl.mYSz; y++)
    {
        for (size_t x = 0; x < tbl.mXSz; x++)
        {
            struct mystruct* sp = getStructPtr(tbl, x, y);
            sprintf(sp->name, "%zu/%zu", x, y);
            sp->id = 1+ y*tbl.mXSz + x;
            sp->status = 1;
        }
    }
}

void main(void)
{
    int size_x, size_y;
    printf("size_x size_y: ");
    scanf("%i %i", &size_x, &size_y);
    struct myTable tbl = createTable(size_x, size_y);

    fill_tbl(tbl);

    int x_selected, y_selected;
    printf("x_selected y_selected: ");
    scanf("%i %i", &x_selected, &y_selected);

    struct mystruct *sPtr = getStructPtr(tbl, x_selected, y_selected);
    strcpy(sPtr->name, "NAME");
    sPtr->id = 1234;
    sPtr->status = 1;

    show_struct(getStructPtr(tbl, x_selected, y_selected));
    show_table(tbl);
}

示例会话

前两行中的“3 2”和“0 1”是用户输入的大小和索引。其余的是输出。

size_x size_y: 3 2
x_selected y_selected: 0 1
OK, STATUS IS 1
name: ->NAME<-
status: 1
*************** x: 0, y: 0******
OK, STATUS IS 1
name: ->0/0<-
status: 1
*************** x: 1, y: 0******
OK, STATUS IS 1
name: ->1/0<-
status: 1
*************** x: 2, y: 0******
OK, STATUS IS 1
name: ->2/0<-
status: 1

*************** x: 0, y: 1******
OK, STATUS IS 1
name: ->NAME<-
status: 1
*************** x: 1, y: 1******
OK, STATUS IS 1
name: ->1/1<-
status: 1
*************** x: 2, y: 1******
OK, STATUS IS 1
name: ->2/1<-
status: 1

相关问题