我想通过使用简单的 void 函数“assignTable”将 2D 字符数组“table”初始化为两个预定义的 2D 字符数组(称为“A”和“B”)之一。然而,虽然数组在“assignTable”中获得了正确的值,但分配的值似乎没有转移到主函数中。我怀疑指针有问题。
你能告诉我我做错了什么吗?
#include <stdio.h>
#include <stdlib.h>
char A[10][10] = {
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
{'A', 'B', 'C', 'D', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 C 读取 BMP 文件并生成该图像的灰度版本。但是,我不知道如何处理行的填充。(回想一下,每行的大小(以字节为单位)必须是 4 的倍数。)当我在每行中每步移动 3 个字节时,如何在填充的字节之前停止?我的代码的关键部分是:
byte pixel[bytes_per_pixel];
for (int i = height - 1; i > 0; i = i - 1)
for (int j = 0; j < width; j = j + 1)
{
fread(pixel, 3, 1, image); // What to do with the padding?
int gray_scale_value = grayScaleConversion(pixel);
converted_image[i][j] = gray_scale_value;
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,这是我到目前为止所得到的内容,您可以在下面找到参考图片。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define HEADER_LOCATION 0x0000
#define WIDTH_LOCATION 0x0012
#define HEIGHT_LOCATION 0x0016
#define BITS_PER_PIXEL_LOCATION 0x001C
typedef short …Run Code Online (Sandbox Code Playgroud) Python 中是否有一种标准方法来生成一个数组(大小为 15),其中随机放置三个 1 和四个 -1,其余数组项为 0?
这种数组的一个例子是
0 0 0 0 1 1 0 -1 1 -1 -1 0 0 0 -1
Run Code Online (Sandbox Code Playgroud) 我有以下简单的程序,它读取以字符串形式给出的数字并打印它。它适用于小数字,但是当我尝试使用大小为 unsigned long 的数字(例如“18446744073709551615”)时,它不会产生预期的结果。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readNumbers(char* numbers, unsigned long numbers_length, unsigned long* number)
{
char string[numbers_length + 1];
strncpy(string, numbers + 0, numbers_length);
*number = strtoul(string, NULL, 10);
}
int main () {
unsigned long number;
char* numbers = "18446744073709551615";
unsigned long numbers_length = strlen(numbers);
readNumbers(numbers, numbers_length, &number);
printf("X = %lu \n", number); // prints 4294967295
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Edit_1:根据此站点, unsigned long 的最大值为 18446744073709551615。
Edit_2:以下代码适用于我的系统:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h> …Run Code Online (Sandbox Code Playgroud) 我想分配一个自定义类型“cell”的二维数组,它是一个结构。但是,我做错了,请参阅下面的代码。你能告诉我我的错误在哪里吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int variable_1;
int variable_2;
int variable_3;
} cell;
void initialiseArray(unsigned long rows, unsigned long columns, cell array[rows][columns])
{
for (int i = 0; i < rows; i = i + 1)
for (int j = 0; j < columns; j = j + 1)
{
array[i][j].variable_1 = 0;
array[i][j].variable_2 = 0;
array[i][j].variable_3 = 0;
}
}
int main()
{
unsigned long rows = 200;
unsigned long columns = 250;
cell* array[rows];
for …Run Code Online (Sandbox Code Playgroud)