gpt4 book ai didi

C 指针和访问数组

转载 作者:行者123 更新时间:2023-11-30 18:53:37 25 4
gpt4 key购买 nike

自从我不得不用 C 语言编程以来已经有一段时间了,我完全忘记了如何使用指针。因此,对于我的项目,我必须编写一个程序来读取 txt 文件并创建数字的直方图。

程序必须是

void hist_2d(
int *head, /* A pointer to the input tiles. */
int w, /* The width (in pixels) of the tile. */
int h, /* The height (in pixels) of the tile. */
int stride, /* Number of pixels between 2 contiguous rows. */
int *bins, /* Array of input bins for the desired histogram. */
int m, /* The number of input bins. */
int *out /* An array in which to store the output histogram. */
)

我的问题是 *head 和访问数组。

例如图像是

[1,2,3,4,5,6;
7,8,9,10,11,12;
13,14,15,16,17,18;
19,20,21,22,23,24]

在pixels.txt中它将是:

1
2
3
...

直方图应该以图 block 为单位计算,因此对于我的示例,图 block 可以是 2x3,即:

[1,2,3;
7,8,9]

做直方图部分很容易得到我不理解的数据。据我了解, *head 是每个图 block 起始点的地址数组,但实际上如何获取该值?以及如何获得下一个值?

head[0] 能给我地址或第一个值吗?

谢谢您,很抱歉这篇文章很长,但我想确保我提供了所需的所有信息。

最佳答案

是的,听起来好像 head 是一个指向 int 数组中第一个元素的指针,其中每个元素(可能)是左上角的索引瓷砖的一角。在 2x3 block 的示例中(如果填充数组的人使用这些数字作为索引),该数组将包含 [1, 4, 13, 16]。

以下是处理数组元素的方法:

int h[4];

h[0] = 1;
h[1] = 4;
h[2] = 13;
h[3] = 16;

printf("%d\n", h[0]); // will print "1"

int n = h[2]; // the value of n is now 13

这样就可以解决问题了吗?

关于C 指针和访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32544313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com