简体   繁体   中英

Array Syntax Confusion in C

I'm trying to teach myself C via the iTunes University/Harvard CS50 course. 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. 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. Adding the value of count to it increments it to the next block, ie, giving the address of the next block. 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. 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) . 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). 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))

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. 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] .

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. (And yes, this "backward" way of indexing exists in real code.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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