[英]Array Syntax Confusion in C
I'm trying to teach myself C via the iTunes University/Harvard CS50 course. 我正试图通过iTunes大学/哈佛CS50课程自学C语言。 In it a program is to be written which resizes a bitmap image file. 在其中编写一个程序来调整位图图像文件的大小。 To do this, I've defined an array (buffer) and written the necessary code for the program to work - it does work. 为此,我已经定义了一个数组(缓冲区)并为程序编写了必要的代码 - 它确实有效。 However, I had to cheat and Google an answer as I couldn't figure it out, and I don't understand a particular piece of syntax in the solution and am hoping someone can help. 然而,我不得不作弊和谷歌一个答案,因为我无法理解,我不理解解决方案中的特定语法,我希望有人可以提供帮助。
The block of code looks like the below and I've put in the comments my specific point of confusion: 代码块如下所示,我在评论中提出了我的具体困惑点:
// allocate array to hold scanline pixels - this is the array I define
RGBTRIPLE *buffer = malloc(bi.biWidth * sizeof(RGBTRIPLE));
// declare variable to track position in buffer array
int count;
// iterate over infile's scanlines
for (int i = 0, height = abs(oldHeight); i < height; i++)
{
// initialize count var to 0
count = 0;
// iterate over pixels in scanline
for (int j = 0; j < oldWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// place pixel in buffer array n times
for (int k = 0; k < n; k++)
{
// below is the confusion. Some sudo code would be great!
*(buffer+(count)) = triple;
count++;
}
}
First the variable buffer
is not an array variable. 首先,变量buffer
不是数组变量。 It is a pointer variable. 它是一个指针变量。 Remember that arrays are not pointers . 请记住, 数组不是指针 。
Now the line 现在行了
*(buffer+(count)) = triple;
is using pointer arithmetic. 正在使用指针算法。 buffer
is a pointer to the RGBTRIPLE
type and after allocating space to it, it is pointing to the first block of that memory. buffer
是指向RGBTRIPLE
类型的指针,在RGBTRIPLE
分配空间后,它指向该内存的第一个块。 Adding the value of count
to it increments it to the next block, ie, giving the address of the next block. 将count
的值添加到它会将其递增到下一个块,即给出下一个块的地址。 Dereferencing this address with *
operator gives the value stored at that address. 使用*
运算符取消引用此地址会给出存储在该地址的值。 It can also be written as 它也可以写成
buffer[count] = triple;
Remember that writing x[i]
is equal to writing *(x+i)
, that is, sum i to the pointer x and read (or write) the value at that address. 请记住,写x[i]
等于写*(x+i)
,即将i求和指针x并读取(或写入)该地址处的值。 So 所以
*(buffer+(count)) = triple;
Is equal to: 等于:
buffer[count] = triple;
Remember also that when you add an integer to a pointer, as in buffer+(count)
, you are incrementing the pointer by count*sizeof(element_of_buffer)
. 还要记住,当您向指针添加整数时,如在buffer+(count)
,您将通过count*sizeof(element_of_buffer)
递增指针。 If you are incrementing a char pointer, this will add 1 to the pointer, if you do the same to an integer pointer in a 32bit machine, this will add 4 to the pointer (4 bytes, the size of an integer). 如果要递增一个字符指针,这将向指针添加1,如果对32位机器中的整数指针执行相同操作,则会向指针添加4(4个字节,整数的大小)。 This is transparent to you, and beacuse of that the pointer will always refer to a valid memory location, that is a multiple of the size of each element. 这对您来说是透明的,并且由于指针总是引用有效的内存位置,即每个元素大小的倍数。
I'm guessing the part you don't understand is this bit: *(buffer+(count))
我猜这部分你不明白这一点: *(buffer+(count))
There are a couple of things to remember, the first being that buffer
is not actually an array, it's a pointer to some memory. 有几件事要记住,第一个是buffer
实际上不是一个数组,它是指向某个内存的指针。 However, pointers can be indexed as arrays, and as arrays decays to pointer you can use an array as a pointer. 但是,指针可以被索引为数组,并且当数组衰减到指针时,您可以使用数组作为指针。
As for the syntax here, *(buffer + count)
is the same as buffer[count]
. 至于这里的语法, *(buffer + count)
与buffer[count]
相同。
Trivia time: Due to the commutative nature of addition, *(buffer + count)
can also be written as *(count + buffer)
, which leads to count[buffer]
being a valid expression. 琐事时间:由于加法的交换性质, *(buffer + count)
也可以写为*(count + buffer)
,这导致count[buffer]
成为有效表达式。 (And yes, this "backward" way of indexing exists in real code.) (是的,这种“后向”索引方式存在于实际代码中。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.