Skip to main content
7 votes
Accepted

Pointers and arrays, don't understand the relationship

OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me. Arrays vs Pointers First, let's learn the difference between arrays and pointers. When you ...
ChrisG's user avatar
  • 7,396
3 votes

in pset4, resize: How does the structs declared in 'bmp.h' know which bytes to get the data from?

How does the structs declared in 'bmp.h' know which bytes to get the data from? The resize program works with the BMP file format which has a few characteristics which need to be altered, namely ...
Tarmyniatur's user avatar
2 votes

in pset4, resize: How does the structs declared in 'bmp.h' know which bytes to get the data from?

fread does really tell the computer where to start getting the data: // read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); // read infile's ...
DinoCoderSaurus's user avatar
2 votes

pset4: Order in structs and "word-aligning" by clang

Arrays vs Structures First off, a struct is not the same thing as an array. structs and arrays are different data types. They have similarities (e.g., they are both complex data types, they can group ...
kzidane's user avatar
  • 17.7k
2 votes
Accepted

Pset 4 - Help understanding structs in copy.c

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 ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

typedef struct declarations?

I found Rob's explanation in this video (starting around 6:40) helpful. He says: we need the identifier next to struct if the definition of the struct is recursive. .... This struct is recursive, ...
DinoCoderSaurus's user avatar
2 votes
Accepted

Problem accessing the last element of the array

If an array contains 27 elements and the first element is index 0, then the last element is index 26. The last index is always one less than the number of elements, because indexes start at 0.
DinoCoderSaurus's user avatar
1 vote

How to merge sort array of struct while maintaining relations between int array and char array

each instance of nameandnum is a complete object, when you do your merge sort you need to swap the whole objects not just the buffer field.
UpAndAdam's user avatar
  • 1,401
1 vote
Accepted

Trouble modifying elements of an array of candidates in Plurality

candidate zero = candidates[0]; creates a new variable of type candidate and copies over the value from candidates[0]. The new variable gets its initial value from candidates[0], but is not connected ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

check50 claiming pairs are sorted incorrectly

I suspect this "introduced another global array called strength_of_victory" is the problem. Recall from the spec: You should not modify anything else in tideman.c other than the implementations ...
DinoCoderSaurus's user avatar
1 vote
Accepted

How does RGBTRIPLE differentiate between the 3 bytes it reads from input file and assign them to rgbt blue, green and red?

When a struct is stored in memory, the data for each struct element is stored in sequence. That means that when a struct's data is read, the data is simply read into each element in the same order ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Declaring a typedef struct for linked list

The requirement is that you give the struct a name so you can use it in the declaration. Whether or not that is a different name isn't important. Both of those declarations are fine.
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Pset5 with linked list: head node creation

An empty linked list consists only of a NULL pointer, no node. There is no dedicated "head" node, just a "head" pointer pointing to the first node (and if there's none, it's NULL). So you could just ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Pset5 Dictionary C Struct Array Call In Load Function Issue

fscanf needs some memory to write to, so char word[LENGTH + 1]; is probably the way to go. You might want to strcpy the result to a new node's word property in case of success. Please note that the ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

This code is written as per video in CS50 week 4 but it is not working

You are always using the subscript 'i'. 'i' has the value 3 when the first loop is finished. You can either change the subscript names in the y loop and the x loop OR just use 'i' again to run those ...
DinoCoderSaurus's user avatar
1 vote

De-referencing an array of pointers to structures

It's a cruel trick by the evil computer!! It's likely that you do understand "all that stuff". The real problem may be scanf! I tried a little repro case, couldn't get it to work (it didn't prompt as ...
DinoCoderSaurus's user avatar
1 vote
Accepted

I am getting an unexpected sizeof() error

You're getting 12 because of something called alignment, which adds internal padding to structures. You can read more about it here: http://c-faq.com/struct/align.html https://en.wikipedia.org/wiki/...
Yuri Laguardia's user avatar
1 vote
Accepted

pset 4 Recover creates 50 corrupted or mainly grey images

First, the problem with the code lies in this line: fwrite(&buf, sizeof(BUFFER), 1, outptr); Adding the & is causing the problem. If you remove it, it will work. If you look at the man page ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Can I create an element of arbitrary size in a STRUCT?

There are any number of ways to implement what you want. The most obvious would be to declare a struct element that is a 508 byte array. This assumes that you really want to implement a struct. But ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Trouble Interpreting Linked List Sample Code - PSET5

first, I wanna make it clear that there is a difference between the address of a pointer and the address stored in a pointer (aka the address that the pointer points to). pointers are special type ...
kzidane's user avatar
  • 17.7k

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