1

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++;
        }
    }
10
  • Is this Harvard course titled "Programming Paradigms"? Or am I confusing it with a Stanford video course? If this is the one where a guy in a blue shirt is talking and drawing to a big blackboard, hit "stop" quickly, he says a lot of nonsense/wrong assumptions/bad practices. Commented Jan 26, 2014 at 16:17
  • 4
    BTW, it's not "sudo code". "Sudo code" would be the source code of the Unix root privilege utility called sudo. The word you are looking for is "pseudo code". Commented Jan 26, 2014 at 16:19
  • @H2CO3; I wanna listen that guy. Seriously! o.O Commented Jan 26, 2014 at 16:26
  • 2
    @haccks See my comments (and the incoming ignorant responses) on this video. Armed with a fresh copy of the C99 Standard, any intermediately experienced C programmer can point out literally dozens of errors in the presentation of this guy. Commented Jan 26, 2014 at 16:29
  • 1
    @H2CO3, I agree, just glanced into that video and this is incredibly fuzzy, imprecise, misleading, wrong ... but almost never correct. Commented Jan 26, 2014 at 16:38

3 Answers 3

2

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, i.e, 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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helps a lot. Just knowing it's called "pointer arithmetic" helps.
1

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.

1 Comment

Thank you. That explanation is awesome and really helps wrap my head around it. Thanks for taking the time to help.
0

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

1 Comment

Thank you for your help! You did a great job of explaining it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.