Hot answers tagged

10

Welcome to the wonderful world of structures! ;-) A structure, or a struct, is a collection of individual vars combined into a group. The group has a variable name, and each element in the struct has a name. To access a particular element in a struct, you access it by structure and element name. Remember that the element names in all structs of the same ...


4

In your example, bf and bi are pointers to two different structures that hold several variables each. These commands will actually write out the indicated full header structures out to the files. Let's walk through one of them: // write outfile's BITMAPFILEHEADER fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr); This command says to write out to the ...


3

First the padding. Keep in mind that the input file and the output file may or may not require the same amount of padding. In item 5, you don't examine the padding in the input file, you skip over it to the beginning of the next line. In item 6, you're adding the appropriate amount of padding to the output file. It uses a loop instead of 4 if statements to ...


2

Yes, you have it right. When a struct is declared, the type, size and location of each var in the struct is remembered. The order of those vars is also important. Physically, each var in the struct is located at a specific offset from the starting address of the struct. Also, each var is located immediately after the previous one within the struct. It's ...


1

Both the i and j counters are used to count passes through the loops. They aren't actually used inside the loops, but they don't need to be. Let's start with the inner loop. biWidth is the number of pixels in a line. j counts from 0 to biWidth, counting by 1 with each pass through the loop. Inside the loop, one pixel is processed in each pass. So, j is ...


1

The notation of the code makes it clear, argv [1] and argv [2] are variables of the pointer type, as the char * infile statement suggests, I do not remember if the theme of the pointers has already been touched on this pset, just say that the variables of type string that are used with the header library of cs50.h, are only a typedef of a variable char * ...


1

Remember that this is copy.c. It's purpose is to merely copy the file, not alter it. The headers are copied, not altered, so it isn't a problem. The value of padding isn't needed to merely copy the headers. However, a lot of this part of the pset's purpose is a setup for the next part, resize.c. When you complete that part, you'll see how going through the ...


1

It's not needed for a padding of 1, 2, or 3 bytes. But imagine what happened if bi.biWidth were a multiple of 4. bi.biWidth * sizeof(RGBTRIPLE) would be a multiple of 4. (bi.biWidth * sizeof(RGBTRIPLE)) % 4 would be 0. 4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4 would be 4, but should be 0. You'd fix that by applying another %4.


1

Unless you've been doing any mallocs (which aren't really needed), my guess is it's because you're not closing all files that you've opened.


1

I really don't understand it very well myself but i am going to try explain it. It is all related to the way this values are stored in the memory, RAM, hard drives, ..., integers, decimal numbers, hexadecimal numbers, ascii characters and more, at the end all are converted to binary in order to be stored in memory, only 0's and 1's. For example: The ascii ...


1

It can't be bfType = 0x424d because that is a completely different number. Yes, it would surely be a different value if it was interpreted as a whole and compared it with each of the 0x42 and 0x4d separately. But what technically happens is something like this /* imagine this is the memory */ b0 b1 b2 b4 ... [0x42][0x4d][....][....] ... ...


Only top voted, non community-wiki answers of a minimum length are eligible