Hot answers tagged

6

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 declare an array as: int nums[20]; you reserve 20 consecutive places in memory, which you can refer to by the variable nums. Specifically, you reserve these places ...


6

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 pointer that actually points to, well, the literal root of the trie. It's critical that you never reassign this pointer. If you do, you'll lose the root of the ...


5

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 something like tasklist_container *cont = malloc(sizeof(tasklist_container)); // 1 allocation for (int i = 0 i < TASKLISTLENGTH; i++) { cont -> list[i] =...


3

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 headers containing information about the data and padding. Supporting this notion, there are a few structures created beforehand in bmp.h. They are simply ...


3

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 you decide to have a pointer to a recursive member inside your struct this way, you won't be able to do that because as long as you're before the semicolon that ...


2

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 elements most probably you will get a segmentation fault. To fix this an option is to declare the char* like this: typedef struct { char name[255]; ...


2

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 answer: there is a difference between a struct type and its pointer type. for example, given the struct struct MyStruct {// members}; MyStruct s; // and ...


2

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 pointer type. it is similar to other native types (e.g., int, char, etc) except that it is a little more complex. when declaring a variable of that type, you ...


2

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 one or more values, etc.) However, there are major differences too, including that they are used in different contexts, array members are of the same data ...


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


2

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, since the definition of struct node contains within it a pointer to a struct node. Notice that we have to say struct node* next inside of the definition of the ...


2

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.


2

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 BITMAPINFOHEADER BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); The first fread reads sizeof(BITMAPFILEHEADER) bytes from the beginning of ...


1

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 that it was stored. Individual elements can be selected out by reading them from their position in the struct. It's all about the location of each element in the ...


1

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

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 use your while loop without any if with special treatment for first node. You forgot its next pointer in that part, but everything is covered by your while. BTW,...


1

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 length of the hashtable can and should be chosen independently of the maximum word length. The hashtable size would be relevant for determining the index to use (...


1

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


1

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 expected) and asked Mr. Google what was wrong ("using scanf in a loop"). From this post (emphasis added): The original code loops indefinitely because the ...


1

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/Data_structure_alignment A quick workaround for this would be to use __attribute__((__packed__)) like you've used in pset4 (see bmp.h there). Like this: #...


1

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 for fwrite, you will see that the first parameter is supposed to be a pointer, not the contents of the memory pointed at, which is what the & triggers. (It'...


1

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 wouldn't it be far simpler to implement an array of 512 bytes instead of a struct? BYTE buffer[512]; You would be able to check the first 4 bytes ...


1

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 of variables that can store memory addresses. where does a pointer store an address? in a memory location somewhere that also has an address. int i = 10; // ...


1

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 vars are the same structure type. The statements about not needing a structure require context. In modifying the headers, my opinion is that the safest way is ...


1

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 with each other, and have corrupted a value that must never be changed. This has led to a domino effect, causing more problems downstream. The issue is that you ...


1

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 the block of data you're reading in is sensible and matches the structure that you will subsequently use to access the individual members of the BITMAPFILEHEADER ...


1

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. For example every integer, except for 0, is considered as true, so if I have while (10) this condition will always be true (and will probably cause an infinite ...


1

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 correct way to do that. you need to allocate memory for each of these pointers first maybe using malloc. see the short on structs for more on how you can access ...


1

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 NOT initialize any of the pointers in the array. Therefore, it does not allocate memory for any of them. So if you tried to access hashtable[10] for example, ...


1

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 say "OK. I don't know what that is!". For structs, on the other hand, if you're trying to declare a variable of a struct type before it's defined, that'll be ...


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