c - 通过函数更改时,主数组中的值不更新
问题描述
我的程序在调用函数后似乎没有更新unsigned char[ ]
in ,因为它打印出的值与调用前的值相同。然而,这不是我的全部代码——我将一般功能复制到了这里。main
changecontents
storage[13]
更新:对不起,我忘了指出 data1[13] 已初始化——索引 0 到至少 200 包含值。
更新 2:添加了 readfile 函数,该函数在 main 中初始化了 data1。
Update3:检查NULL
readfile 中 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;
}
解决方案
更新后的代码不在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.c
为arr41
使用:
/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
同样,这显然是正确的。
推荐阅读
- python - 使用 string.decode("utf-8","ignore") 时结果错误
- python - 预测模型需要 3 维但形状不匹配的数组
- google-apps-script - 覆盖文件 Google 表格脚本
- mongodb - 无法使用 Golang 将记录保存到 Mongodb
- python - tensorflow2.1 InvalidArgumentError:断言失败:[0] [Op:Assert] 名称:EagerVariableNameReuse
- swagger - Swagger openapi spec 3.0 结果 None 不是对象类型
- c - 这两者的优缺点是什么(表示不同类型数据的通用数据结构)?
- jquery - 使用每个针对特定元素的问题
- android-studio - Android Studio 不运行 Flutter 应用程序
- python - 如何使用 Matplotlib 在 Latex 中结合新行以粗体书写字符?