修改struct数组中struct的成员

时间:2018-01-25 22:03:25

标签: c arrays struct member

我正在尝试解决我被赋予的任务中的问题 - 问题在于我正在实现的其中一个功能不起作用。

首先,有人告诉我,我的教授提供的代码(下面的代码)不能以任何方式修改:

#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-&gt; frames []中的fileName结构成员时,它都会显示读取访问冲突。仔细检查后,似乎来自pAnimation-&gt; frames []的地址中的fileName是未知的。

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

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

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

我也接触过我的教授,他说我不需要分配任何东西,除非我“添加”一个新的框架(通过insertFrame)。我不完全确定他的意思。

2 个答案:

答案 0 :(得分:1)

看着

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来电的结果。

我会在insertFrame()中放置2帧分配调用,就像创建/添加新帧一样。

答案 1 :(得分:0)

我相信你需要改变

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

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

因为pAnimation结构成员frames是类型ppFrames,这意味着它是指向Frame指针的指针,而不是指向Frame的指针(它是Frame指针的数组,而不是Frame s的数组。

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

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

否则,Frame数组中的frames指针不会指向任何实际内存,只要您尝试访问其成员,就会遇到访问冲突。

相关问题