struct

Accessing elements within a pointer of a struct inside another pointer to a struct

£可爱£侵袭症+ 提交于 2019-12-23 01:24:10
问题 Just trying to learn how nested pointers to structs work. How would I access an element within a pointer of a struct within another pointer to a struct? Thanks. #include <iostream> typedef struct { unsigned int a; unsigned int b; unsigned int c; } inner; typedef struct { unsigned int d; unsigned int e; inner * in; } outer; /* Function to use struct */ void test(outer * out) { //Update the value of out->in->a. out->in->a = 5; } int main() { outer * out; test(out); std::cout << "My updated

Using a struct across classes in c++

折月煮酒 提交于 2019-12-22 22:19:00
问题 I am still fairly new to c++ so please allow! Essentially I have created a struct in the header file for one of my classes full of strings. typedef struct details{ string name; string address; }details; and I wish to not only use this struct in the cpp file that belongs to the header, but in other classes as well. For example, I want to create a vector of this struct in another header file. private: vector <details> foo; I also want to use the struct in the main details d; d.name = "hi"; d

Should I use a Struct instead of a lightweight data class for my Linq2Sql data?

你。 提交于 2019-12-22 18:12:27
问题 I often take the classes that linq2sql generates and create a simple data-only class like so public class myentity { public Guid id { get; set; } public string name { get; set; } // etc } I don't put methods in these classes and I mainly use them as helper classes so I can serialize to/from json and other similar actions, quite easily. My question is, should I use a struct in this case rather than a class? It seems to make sense to make it a struct as its more or less the definition of a

Assigning an address to a struct pointer array member in C

柔情痞子 提交于 2019-12-22 17:48:36
问题 Having considerable trouble with some pointer arithmatic. I think I get the concepts (pointer variables point to a memory address, normal variables point to data) but I believe my problem is with the syntax ( *, &, (*), *(), etc.) What I want to do is build dynamic arrays of a custom struct (i.e. arrays of pointers to heap structs), and my interface provides two methods, "ad_to_obj_array" (which takes the object to add and the array which can be null for empty) and "obj_array_dustbin" (which

Modifying a struct passed as a pointer - C

一世执手 提交于 2019-12-22 17:38:42
问题 I'm trying to modify a struct that was passed as a parameter by using a pointer by I can't get it working. I cant just return the struct because the function must return an integer. How can I modify the struct in the function? This is what I've done so far: typedef enum {TYPE1, TYPE2, TYPE3} types; typedef struct { types type; int act_quantity; int reorder_threshold; char note[100]; }elem; int update_article(elem *article, int sold) { if(*article.act_quantity >= sold) { article.act_quantity =

Struct and pointer to pointer

◇◆丶佛笑我妖孽 提交于 2019-12-22 17:11:19
问题 I am learning about linked lists and how to create them in C with structs and pointers. I have an example below. From my understanding the called push() passes the beginning memory location of our struct where the head node lies as an argument. The parameter of our push() function takes a struct node as a pointer to pointer so it is passed as a reference, not an actual copy. So the first pointer of our struct node** headref is just a pointer to the memory location of our head node and the

Can main() return structure?

陌路散爱 提交于 2019-12-22 11:27:01
问题 Yesterday in the interview one question was asked to me that can main return struct ? I have no idea can any one please tell me is it possible or not,if yes why? 回答1: main can only return an int value in C (at least for hosted implementations ). 回答2: Section 5.1.2.2.1 of the C standard says no: The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /

can I access a struct inside of a struct without using the dot operator?

£可爱£侵袭症+ 提交于 2019-12-22 10:58:09
问题 I have 2 structures that have 90% of their fields the same. I want to group those fields in a structure but I do not want to use the dot operator to access them. The reason is I already coded with the first structure and have just created the second one. before: typedef struct{ int a; int b; int c; object1 name; } str1; typedef struct{ int a; int b; int c; object2 name; } str2; now I would create a third struct: typedef struct{ int a; int b; int c; } str3; and would change the str1 and atr2

Inline member operators vs inline operators C++

别等时光非礼了梦想. 提交于 2019-12-22 10:37:34
问题 If I have two structs: struct A { float x, y; inline A operator*(A b) { A out; out.x = x * b.x; out.y = y * b.y; return out; } } And an equivalent struct struct B { float x, y; } inline B operator*(B a, B b) { B out; out.x = a.x * b.x; out.y = a.y * b.y; return out; } Would you know of any reason for B's operator* to compile any differently, or run any slower or faster than A's operator* (the actual actions that go on inside the functions should be irrelevant)? What I mean is... would

How to sort a vector of structs based on a vector<string> within the vector to be sorted?

眉间皱痕 提交于 2019-12-22 09:46:40
问题 What is the best way to alphabetically sort a vector of structures based on the first word in every vector of all the structures in the vector of structures? struct sentence{ vector<string> words; }; vector<sentence> allSentences; In other words, how to sort allSentences based on words[0]? EDIT: I used the following solution: bool cmp(const sentence& lhs, const sentence & rhs) { return lhs.words[0] < rhs.words[0]; } std::sort(allSentences.begin(), allSentences.end(), cmp); 回答1: Provide a