简体   繁体   English

C指针和访问数组

[英]C pointers and accessing arrays

It's been a while since I've had to program in C and I have completely forgotten how to work with pointers. 自从我不得不使用C编程以来已经有一段时间了,我完全忘记了如何使用指针。 So for my project I have to make a program that reading in a txt file and create a histogram of the numbers. 因此,对于我的项目,我必须编写一个程序以读取txt文件并创建数字的直方图。

The program must be 该程序必须是

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. */
)

My question is with *head and accessing the array. 我的问题是* head和访问数组。

So for example the image is 例如,图像是

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

In pixels.txt it would be: 在pixel.txt中,它将是:

1
2
3
...

The histogram is supposed to calculated in tiles, so for my example a tile could be a 2x3 which would be: 直方图应该以图块计算,因此在我的示例中,图块可以是2x3,即:

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

Doing the histogram part is easy its getting they data that I don't understand. 进行直方图部分很容易,因为它可以获取我不了解的数据。 From what I understand *head is an array of addresses for the starting point of each tile but how do actually get the value? 据我了解,* head是每个磁贴起始点的地址数组,但实际上如何获得该值? and how do I get the next value? 以及如何获得下一个值?

Would head[0] get me the address or the first value? head [0]会给我地址还是第一个值?

Thank you, sorry for the long post, but I wanted to make sure I gave all the info needed. 谢谢,很抱歉发布了这么长时间,但是我想确保我提供了所有需要的信息。

Yeas, it sounds as if head is a pointer to the first element in an array of int , where each element is (perhaps) the index of the upper left corner of a tile. 是的,听起来好像head是指向int数组中第一个元素的指针,其中每个元素都是(也许是)图块左上角的索引。 In your example of the 2x3 tiles (if whoever filled the array is using those numbers as indices), the array would contain [1, 4, 13, 16]. 在您的2x3磁贴示例中(如果填充数组的人使用这些数字作为索引),则数组将包含[1、4、13、16]。

Here's how to handle the elements of an array: 这是处理数组元素的方法:

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

Does that clear things up? 这样可以解决吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM