[英]C pointers and accessing arrays
自從我不得不使用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]
在pixel.txt中,它將是:
1
2
3
...
直方圖應該以圖塊計算,因此在我的示例中,圖塊可以是2x3,即:
[1,2,3;
7,8,9]
進行直方圖部分很容易,因為它可以獲取我不了解的數據。 據我了解,* head是每個磁貼起始點的地址數組,但實際上如何獲得該值? 以及如何獲得下一個值?
head [0]會給我地址還是第一個值?
謝謝,很抱歉發布了這么長時間,但是我想確保我提供了所有需要的信息。
是的,聽起來好像head
是指向int
數組中第一個元素的指針,其中每個元素都是(也許是)圖塊左上角的索引。 在您的2x3磁貼示例中(如果填充數組的人使用這些數字作為索引),則數組將包含[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
這樣可以解決嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.