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 ...
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 ...
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 ...
2
votes
Accepted
Want to make sure I understand struct, malloc, and pointers and how they relate to each other
I originally didn't alloc for the struct, just declared it. It worked,
and from what I understand, it worked because it was created in the
stack? Am I correct?
the type BITMAPINFOHEADER is not a ...
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 ...
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, ...
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 ...
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.
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 ...
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.
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 ...
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 ...
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 ...
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.
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 ...
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/...
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 ...
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 ...
1
vote
Accepted
PSET4 Resize: How do I copy a struct?
Oh, you're going to hate me after you read this, or you're going to slap yourself in the forehead and yell "DOH!"
BITMAPINFOHEADER new_bi = bi;
This copies the ENTIRE structure, provided that both ...
1
vote
Pset4 Resize: Am I on the right track here? None of my code seems to be working. Any advice would be great!
Because of the number of errors and cascade effects, I'm going to point you in the right directions so that you can work on the problems.
Your biggest problem is that you have confused two fields ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
structs × 38pset4 × 11
pset5 × 6
pointers × 5
array × 5
malloc × 5
pset3 × 4
recover × 4
resize × 4
c × 4
speller × 3
pset6 × 2
dictionary × 2
final-project × 2
linked-list × 2
variables × 2
bmp × 2
node × 2
typedef × 2
cs50x × 1
check50 × 1
load × 1
segmentation-fault × 1
trie × 1
hash-table × 1