Hot answers tagged

6 votes
Accepted

Pset5: Misunderstanding tries and method to assign letter to an array inside a struct

Your understanding is correct, but your example has a dangerous flaw if you were to implement it. Look back at your question as a whole. You have two important variables, root and newptr. root is the ...
user avatar
  • 63.1k
6 votes
Accepted

How to free a nested struct?

When you allocate memory by calling malloc(), and you want to free that memory, you have to call free() on every pointer that you initialized it with a call to malloc(). For example, if you have ...
user avatar
  • 17.4k
6 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 ...
user avatar
  • 7,326
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 ...
user avatar
3 votes
Accepted

typedef struct vs. struct

Yes. The first version typedef struct { // members } Point; defines an anonymous struct (i.e., a struct that doesn't have a name) and gives it a type name (i.e., Point). Unfortunately, if ...
user avatar
  • 17.4k
3 votes
Accepted

Why can we pass a struct variable to a function by value but not an array?

As taught we can pass a whole "struct" by value to a function but we can't pass an array by value. Why? the short answer is: because structs and arrays are implemented differently in C. longer ...
user avatar
  • 17.4k
2 votes
Accepted

Question of inputing an array of char

First in the struct definition, you are the declaring a pointer to a char (char*), so if you don't want to use GetString(), when you try to store something that is not a pointer on any of this ...
user avatar
  • 1,359
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 ...
user avatar
  • 17.4k
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 ...
user avatar
  • 17.4k
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 ...
user avatar
  • 63.1k
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, ...
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.
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 ...
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 ...
user avatar
  • 63.1k
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.
user avatar
  • 18.3k
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 ...
user avatar
  • 20.7k
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 ...
user avatar
  • 20.7k
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 ...
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 ...
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/...
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 17.4k
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 63.1k
1 vote
Accepted

Pset4: How does fread add properly to the struct?

fread() knows nothing about the structure. All it knows is the size of the block to read and the address of the memory block where it will put the bytes it reads. It's up to you to make sure that ...
user avatar
  • 18.3k
1 vote
Accepted

Bang and Structs

Whatever you put inside the parenthesis is considered/has to be a condition. So it could be while (a == 5) or if a variable is itself a condition you can put the variable directly in the parenthesis. ...
user avatar
  • 7,326
1 vote

Struct array not assigning value being set

input_array is an array of pointers to inputs — input_array[counter] evaluates to an input * not an input. accessing input_value through input_array[counter] using the . operator is not actually the ...
user avatar
  • 17.4k
1 vote
Accepted

Am I creating an array of pointers or an array of structs?

Does node* hashtable[500]; create an array of pointers or does it create an array of nodes ? It creates an array of node *s (otherwise known as pointers to nodes). The array declaration above does ...
user avatar
  • 17.4k
1 vote
Accepted

placement of structs in code

We do that for functions in order for the compiler to know that there's a function with this signature that exists in our source code so that it doesn't get to a function call before it's defined and ...
user avatar
  • 17.4k

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