struct

Why use dynamic memory allocation(i.e. malloc()) when implementing linked list in c?

我只是一个虾纸丫 提交于 2021-02-15 05:34:57
问题 Okay this question may sound stupid to the amateur programmers . But seriously this is bothering me and a solemn answer to this doubt of mine is welcomed. I have just started to take my first ever course in data structures. And what is bothering me is this: Assuming C is used, //Implementing a node struct Node { int data; struct *Node; }; Now while creating a node why do we use the dynamic memory allocation technique where we use malloc(). Can't we just create a variable of type ' Struct Node

Why does my function not modify the value of variable in a struct in C?

て烟熏妆下的殇ゞ 提交于 2021-02-13 17:41:59
问题 This is my input. I must write a function, which give me minus Zesp.Im struct Zesp { double Re; double Im; }; struct Zesp z1 = { .Re = 5.323 ,.Im= 3.321}; typedef struct Zesp zesp; zesp spZ(zesp z) { z.Im = -(z.Im); return z; } int main () { spZ(z1); printf("%.2f, %.2f\n", z1.Re, z1.Im); return 0; } I don't know why I get 3.321 instead of -3.321? I edit my program, my teacher said that I can't modify argument of a function spZ. I get a segmentation fault #include <stdio.h> struct Zesp {

Why does my function not modify the value of variable in a struct in C?

痴心易碎 提交于 2021-02-13 17:41:36
问题 This is my input. I must write a function, which give me minus Zesp.Im struct Zesp { double Re; double Im; }; struct Zesp z1 = { .Re = 5.323 ,.Im= 3.321}; typedef struct Zesp zesp; zesp spZ(zesp z) { z.Im = -(z.Im); return z; } int main () { spZ(z1); printf("%.2f, %.2f\n", z1.Re, z1.Im); return 0; } I don't know why I get 3.321 instead of -3.321? I edit my program, my teacher said that I can't modify argument of a function spZ. I get a segmentation fault #include <stdio.h> struct Zesp {

Why do we declare pointer to the same struct in Linked Lists

和自甴很熟 提交于 2021-02-11 17:58:21
问题 struct Node { int value; Node *next; Node *prev; }; This is the creation of a linked list. My question is that why do we declared the pointer of the same struct. e.g Node *next . If we are going to have a linked list of integers, can we just use int * next and int *prev instead of using Node *next . 回答1: Each element in a linked list contains not only the data ( int in your case), but also a pointer to the next element (and in a doubly-linked list, like you have, a pointer to the previous

constexpr construction of a POD struct with a char[] field

偶尔善良 提交于 2021-02-11 15:52:06
问题 How can I make the function below constexpr ? This is a function that I use to create a POD struct that is defined in a "C" header file that I cannot change. I ended up with the following static function helper (which works) to make these structs, I believe it is not constexpr , as the strncpy call violates constexpr . static auto makeWPTEntry( const char* name, double aLatDeg, double aLatMin, double aLonDeg, double aLonMin, char bSeven = false, double aCourse = 0, double aAzimuth = 0, double

Passing struct pointers to functions

◇◆丶佛笑我妖孽 提交于 2021-02-11 15:27:29
问题 Hey I'm having trouble understanding on how structs get passed to functions as pointers because i want to modify my structs later in my project. So I'm creating a game that has rooms and this function creates a 2d array of the world. I'm passing in struct room and struct World because I'm storing each room in a cord in World struct which is a 2d array. When i got print out each room, it doesn't print anything out and i think the problem is with my pointers. So my first function that's in a

C error Variably modified at the file scope [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-11 15:12:05
问题 This question already has answers here : Variably modified array at file scope (6 answers) Variably modified 'variable_name' at file scope error? (1 answer) Closed 7 years ago . yeah so im new to c and i came across an error when dealing with the following code.: typedef struct{ int head; int length; Customer customer[MAX_LENGTH]; } CustomerCi; and the error that came out was: "error: variably modified 'customer' at the file scope" i have no idea how to fix this and any help would be

C error Variably modified at the file scope [duplicate]

心不动则不痛 提交于 2021-02-11 15:05:29
问题 This question already has answers here : Variably modified array at file scope (6 answers) Variably modified 'variable_name' at file scope error? (1 answer) Closed 7 years ago . yeah so im new to c and i came across an error when dealing with the following code.: typedef struct{ int head; int length; Customer customer[MAX_LENGTH]; } CustomerCi; and the error that came out was: "error: variably modified 'customer' at the file scope" i have no idea how to fix this and any help would be

Reading from txt file into a struct

本秂侑毒 提交于 2021-02-11 14:29:35
问题 Trying to read from txt file until a semicolon and will store it into a array inside struct. struct Total { char *Name; int *No; }MyCities; This is my struct made the arrays pointers so i can allocate memory later. Depending on the content of the txt. Each line is a entry for the struct. London;15 Oslo;12 vienna;35 Let's say this is the content of txt file, it will read the city name into MyCities.Name and the number after the semicolon into MyCities.No FILE *fp; char line; fp = fopen("City

How to fix System.TypeLoadException when declaring structs with union?

浪尽此生 提交于 2021-02-11 14:17:16
问题 Receiving System.TypeLoadException in runtime. I use code from third-party source to define structs and p-invoke external methods. I found the full code online in this link. In the code there are some structs with union, using FieldOffsetAttribute(0) on multiple fields. when I try to use some of the structs I get an exception: Could not load type 'NET_DVR_SLAVE_CHANNEL_UNION' from assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object