修改结构数组中结构中的成员

Modifying a member in a struct within a struct array

我正在尝试解决分配给我的任务中的问题 - 问题是我当前正在实现的功能之一不起作用。

首先,我被告知我的教授(在下方)提供的代码不能以任何方式修改:

#include <crtdbg.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#define BLOCK_SIZE 64
    typedef enum { FALSE = 0, TRUE } BOOL;

typedef struct {
    char* fileName;         // Frame fileName
}Frame, *pFrame, **ppFrame;

typedef struct {
    unsigned int numFrames;     // number of Frame* 
    ppFrame frames;         // array of Frame*
}Animation, *pAnimation;

// Forward declarations
void initAnimation(pAnimation);
void insertFrame(pAnimation);
void deleteFrames(pAnimation);
void runFrames(pAnimation);

int main(void)
{
    char response;
    BOOL RUNNING = TRUE;
    Animation A;
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    initAnimation(&A);

    while (RUNNING)
    {
        printf("MENU\n 1. Insert a Frame\n 2. Delete all the Frames\n 3. Run the Animation\n 4. Quit\n");
        scanf("%c", &response);
        switch (response)
        {
        case '1':insertFrame(&A); break;
        case '2':deleteFrames(&A); break;
        case '3':runFrames(&A); break;
        case '4':RUNNING = FALSE; deleteFrames(&A); break;
            default:printf("Please enter a valid option\n");
        }
    printf("\n");
    while ((response = getchar()) != '\n' && response != EOF);// clear input buffer
}
return 0;
}

接下来,我需要为提供的代码中的前向声明语句创建函数。我遇到问题的函数在这里:

void insertFrame(Animation *pAnimation)
{
    char input[50];
    int count;
    int dupe = 0;
    int blockFrames = pAnimation->numFrames % BLOCK_SIZE;
    int blocks = (pAnimation->numFrames / BLOCK_SIZE) + 1;

    printf("Insert a frame into the Animation\n");
    scanf("%s", input);

    /* check the input to see if it matches any of the existing frames */
    for (count = 0; count < pAnimation->numFrames; count++)
    {
        printf("%s\n", pAnimation->frames[count]->fileName); /*test: show the file name to be compared against*/
        if (strcmp(input, pAnimation->frames[count]->fileName) != 0)
        {
            dupe = 1;
            printf("Dupe detected!");
            return;
        }
    }

    printf("dupe = %d\n", dupe);

    /* afterwards, actually add the frame if it's ok */
    if (dupe == 0)
    {
        pAnimation->numFrames++;
        strcpy(pAnimation->frames[pAnimation->numFrames - 1]->fileName, input); /* <-- This is where the error happens */

    }
}

每次我使用 printf 或 strcpy 显示或修改 pAnimation->frames[ ] 中的 fileName 结构成员时,它都会显示读取访问冲突。仔细检查后,似乎来自 pAnimation->frames[ ] 指向的地址的文件名是未知的。

为什么会发生这种情况,我该如何解决?


好的,所以在听取了建议之后,这就是我的 initAnimation 的样子:

void initAnimation(Animation *pAnimation)
{
    pAnimation->numFrames = 0;
    pAnimation->frames = malloc(BLOCK_SIZE * sizeof(Frame));
}

我也联系了我的教授,他说我不需要分配任何东西,除非我是 "adding" 一个新框架(通过 insertFrame)。我不完全确定他的意思。

我相信你需要改变

pAnimation->frames = malloc(BLOCK_SIZE * sizeof(Frame));

pAnimation->frames = malloc(BLOCK_SIZE * sizeof(pFrame));

因为 pAnimation 结构成员 framesppFrames 类型,这意味着它是指向 Frame 指针的指针而不是指向 Frame 的指针](它是一个 Frame 指针数组,而不是一个 Frame 指针数组)。

我相信您还需要为函数中的每个 Frame 指针分配 space,如下所示:

for (int i = 0; i < BLOCK_SIZE; ++i) {
  pAnimation->frames[i] = malloc(BLOCK_SIZE * sizeof(Frame));
}

否则,frames 数组中的 Frame 指针将不会指向任何实际内存,并且每当您尝试访问其成员时都会遇到访问冲突。

正在看

void initAnimation(Animation *pAnimation)
{
    pAnimation->numFrames = 0;
    pAnimation->frames = malloc(BLOCK_SIZE * sizeof(Frame));
}

我假设您正在分配一个帧指针数组。所以每个单元格都是 sizeof(pFrame) 而不是 sizeof(Frame)。这足以容纳指向框架的指针而不是框架本身。

其次,您需要在创建这些框架时对其进行分配。

类似

pAnimation->frames[i] = malloc(sizeof(Frame));

最后一个 Frame 保存了一个指向字符数组的指针。你也需要分配它

pAnimation->frames[i]->fileName = malloc(50); // Use your max size

您还应该检查 malloc 调用的结果。

我会把 2 个帧分配调用放在 insertFrame() 中,因为当你是 creating/adding 一个新帧时。