首页 > 解决方案 > 通过函数更改时,主数组中的值不更新

问题描述

我的程序在调用函数后似乎没有更新unsigned char[ ]in ,因为它打印出的值与调用前的值相同。然而,这不是我的全部代码——我将一般功能复制到了这里。mainchangecontentsstorage[13]

更新:对不起,我忘了指出 data1[13] 已初始化——索引 0 到至少 200 包含值。

更新 2:添加了 readfile 函数,该函数在 main 中初始化了 data1。

Update3:检查NULLreadfile 中 fopen() 函数的返回值;文件.gif 存在。

#include <stdio.h>
int main()
{
    unsigned char data1[4000] = {0};  
    readfile(data1, 4000, "file.gif");    // filled 3022 indices in data1; file.gif exists
    printcontents(data1);       // prints the array contents as desired here 
    changecolor(data1);     // doesn’t change the array contents 
    printcontents(data1);       // prints the same array contents as original      
    return 0;
}
int printcontents(unsigned char storage[ ])
{
    printf(“%X”, storage[13]);  // data1 passed in function call in main
    return 0;
}
int changecolor (unsigned char storage[ ])
{
    storage[13] = storage[13] >> 2;     // data1 passed in function call in main
    return 0;
}
int readfile(unsigned char storage[ ], int capacity, char filename[ ])
{
    FILE * content = fopen(filename, "r");
    if (content != NULL)
    {
        size_t numElements, bytesPer = sizeof(unsigned char);
        fseek(content, 0, SEEK_END);
        numElements = ftell(content);
        rewind(content);
        fread(storage, bytesPer, numElements, content);   // returned 3022
        fclose(content);
    }
    return 0;
}

标签: c

解决方案


更新后的代码不在capacity. readfile()该函数在打开文件失败时不会明确报告错误——这花费了我一些时间。

这是您的代码的一个变体,它产生我期望的输出:

#include <stdio.h>
int printcontents(unsigned char storage[ ]);
int changecolor (unsigned char storage[ ]);
int readfile(unsigned char storage[ ], size_t capacity, char filename[ ]);
int main(void)
{
    unsigned char data1[4000] = {0};  
    readfile(data1, 4000, "file.gif");    // filled 3022 indices in data1; file.gif exists
    printcontents(data1);       // prints the array contents as desired here 
    changecolor(data1);     // doesn’t change the array contents 
    printcontents(data1);       // prints the same array contents as original      
    return 0;
}
int printcontents(unsigned char storage[ ])
{
    printf("%#X\n", storage[13]);  // data1 passed in function call in main
    return 0;
}
int changecolor (unsigned char storage[ ])
{
    storage[13] = storage[13] >> 2;     // data1 passed in function call in main
    return 0;
}
int readfile(unsigned char storage[ ], size_t capacity, char filename[ ])
{
    FILE * content = fopen(filename, "r");
    if (content != NULL)
    {
        size_t numElements, bytesPer = sizeof(unsigned char);
        fseek(content, 0, SEEK_END);
        numElements = ftell(content);
        if (numElements > capacity)
            numElements = capacity;
        rewind(content);
        if (fread(storage, bytesPer, numElements, content) != numElements)
            printf("Bogus input\n");    // returned 3022

        printf("Got %zu bytes\n", numElements);
        for (int i = 0; i < 16; i++)
            printf(" 0x%.2X", storage[i]);
        putchar('\n');
        fclose(content);
    }
    else
        fprintf(stderr, "failed to open file '%s'\n", filename);
    return 0;
}

我使用随机数生成器来创建file.gif文件。当我在该文件上运行程序时,我得到:

Got 3023 bytes
 0x62 0x66 0x74 0x77 0x76 0x62 0x66 0x6E 0x6D 0x72 0x70 0x62 0x63 0x66 0x71 0x7A
0X66
0X19

如您所见,数组第 13 位的值(0x63 和 0x71 之间)从 0x66(102)变为 0x19(25),这是正确的“除以 4”值。

从这里无法猜测您的版本出了什么问题。最简单的猜测是该文件file.gif不存在,尽管您的断言相反。您的代码没有显示fread()成功。

我所做的大部分更改都是使代码通过我的默认编译器警告所必需的。我clang在 Mojave 10.14.1 上使用 XCode 10.1,并编译arr41.carr41使用:

/usr/bin/clang -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes \
    -Wstrict-prototypes arr41.c -o arr41

我还添加了诊断代码以在文件未打开时发出警告,并打印读取的数据量,该数据的前 16 个字节,并整理打印值的格式(通常用换行符结束输出)。

我顺便指出,这不是 MCVE(最小、完整、可验证的示例)。MCVE 可能是:

#include <stdio.h>

static void printcontents(unsigned char storage[]);
static void changecolor(unsigned char storage[]);

int main(void)
{
    unsigned char data1[20] = "ABCDEFGHIJKLMNOPQRS";
    printcontents(data1);
    changecolor(data1);
    printcontents(data1);
    return 0;
}

static void printcontents(unsigned char storage[])
{
    printf("%#X\n", storage[13]);
}

static void changecolor(unsigned char storage[])
{
    storage[13] = storage[13] >> 2;
}

输出:

0X4E
0X13

同样,这显然是正确的。


推荐阅读