Questions tagged [struct]

1

votes
2

answer
292

Views

Can embedded struct method have knowledge of parent/child?

I have been working with Go on and off on my free time for a few months and I feel I've been getting a hang of it. Coming from traditional OOP languages, such as Java and PHP, I had a hard time grasping composition and embedding in Go. I think it finally clicked recently and want to use it more, b...
ZeroHour
0

votes
0

answer
9

Views

How to read in different multiple data types from input file c++?

I am trying to read in data from a file and I don't think it is working because when I print everything is blank. I have an array of structures and I am trying to read in values from a file into different structures in the indexes of the array. file looks like 8 Cola 1.25 25 Root-beer 1.25 20 Lemon...
Khizar M
-1

votes
1

answer
19

Views

Putting objects into Priority Queue in C++ causing invalid operands to binary expression

I want to create a priority queue in C++ I created a struct as a general blueprint for instantiating a Priority Queue template struct PriorityQueue { typedef std::pair PQElement; std::priority_queue elements; inline bool empty() const { return elements.empty(); } inline void put(T item, priority_t...
cruise_lab
2

votes
2

answer
20

Views

Is it bad practice to store a struct member value in local var with a shorter name?

I used a struct to store some data that I read from a file. The struct members are a float and an array of unsigned integers. After the file is read none of the values will ever change. The value I want to store locally is is one of the unsigned integers contained in the struct member array. Is it...
Shwig
2

votes
1

answer
32

Views

Why C# Struct behave different as Attributes? (compare to declared as field)

I understand struct is value type. But I do not understand why it behave like this? Is it because i didn't treat it as immutable? or is it has something to do with the auto property? using System; namespace StructQuestion { class Program { static StructType structAsProperty { get; set; } static Stru...
Victor Cash
2

votes
2

answer
41

Views

How to initiliaze a dynamic 2D array inside a struct in c?

I want to use a struct to contain some data and passing them between different functions in my program,this struct has to contain a dynamic 2D array (i need a matrix) the dimensions change depending on program arguments. So this is my struct : struct mystruct { int **my2darray; } I have a function...
Adl
1

votes
2

answer
223

Views

Invalid Initializer - Array of structs

I am having problems with assigning a value to cv. Based on my code below, I am getting error: invalid initializer at the line: ChVec cv = r->cv; Would anyone know what the issue might be? Thanks for your help. tuple.c #include "defs.h" #include "tuple.h" #include "reln.h" #include "hash.h" #include...
Archer
1

votes
0

answer
39

Views

Why is the first element of the struct array at index 0 empty after writing and reading it from file?

I'm trying to output the name of the first element if the struct array at index 0. I tried making an empty index 0 of the struct array, and moving the order up so that RC COLA will be (item+1). It does show up the first time I run the program. but when I open it again, it the first element of index...
0

votes
1

answer
27

Views

Segmentation Fault while trying to read next line/character from file

I have a school project where I need to read .ppm file in C and store it in a struct to be used for later tasks. Once I get the first line and assign it to a struct variable, I get errors if I try to navigate through the file again. Here is my code: #include #include typedef struct { int r, g, b;...
Naeem Khan
-2

votes
1

answer
40

Views

How to define and declare a struct when the constructor is used for a default argument?

I have a .h file inside I have a function that uses a Struct/Class Constructor for the default argument. It appears on the declaration end like answered here : Where to put default parameter value in C++? Function declaration vector scan(const ScanOptions& scan_options = ScanOptions()); Struct defi...
Ojav
1

votes
2

answer
221

Views

How to instantiate a public tuple struct(with private field) from a different module?

I have a module where a tuple struct is defined as: #[derive(Clone, Default, Eq, Hash, PartialEq, PartialOrd)] pub struct Id(Vec); I make use of this struct in another module which needs to be imported there. But when I try to instantiate this struct Id as: let mut id = Id(newId.as_bytes().to_vec())...
Rajeev Ranjan
1

votes
2

answer
47

Views

C - Cannot get stack to initiate in my program

I am trying to create a stack but I am having a problem initiating it. The code that I have is: #define LINELN 72 #define STACKSZ 25 #define NEWLN '\n' #include #include // interface struct for stack typedef struct stack { char data[STACKSZ]; int top; } stack; voi...
Lagging
0

votes
1

answer
24

Views

C Programming: String arrays - how to check equality?

I have a struct such as this: struct car{ char parts[4][10]; }; I initialize them in my main() function, as though: char fill[10] = "none"; struct car c; int i = 0; for (i; i< 4; i++){ memcpy(c.parts[i], fill, 10); } At this point, each string in the array has "none", like this: int j = 0; for (j; j...
Darrel Gulseth
0

votes
1

answer
25

Views

How to get reference to a rectangle from List [duplicate]

This question already has an answer here: Changing the value of an element in a list of structs 4 answers I want to get pointer to the rectangle from list. But Rectangle is struct and when I try to get pointer I get copy. Class MyClass { Rectangle MovingRectangle; List RectangleList; Point StartLoc...
super sahar
0

votes
1

answer
23

Views

How to get reference to a rectangle from List

I want to get pointer to the rectangle from list. But Rectangle is struct and when I try to get pointer I get copy. Class MyClass { Rectangle MovingRectangle; List RectangleList; Point StartLocation; ***some code here*** void PicBoxMouseDown(object sender, MouseEventArgs e) { foreach (rectangle in R...
super sahar
2

votes
2

answer
18

Views

Problem implementing reciprocally nested structures in Julia

I am trying to define two structs, Node and Edge. A node contains an array of edges, while an edge contains the destination node and the probability of reaching that node. Because of how the rest of the problem is structured, I cannot avoid having the Edge object. struct Node edges::Vector{Edge} end...
-1

votes
0

answer
20

Views

Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeeb708bc)

So basically the code segment in my project (Snake game with sdl2) which causes the problems is (1)game -> board[game -> snake[0].x + 1 + game -> snake[0].y + 1* CELLS] = HEAD; for(int i = 1; i < game -> snake_length; i ++){ game -> board[game -> snake[i].x + game -> snake[i].y * CELLS] = TAIL; }...
Fo Young Areal Lo
0

votes
1

answer
22

Views

How to use structs in binary search?

I'm trying to learn c (its my first language) and i have some problems with this task. (It is no homework!) Task: Implement the binary search algorithm on an array of structs of the following kind (sorry for my English). struct { unsigned int number; char* food; int price; } pk; The given array is...
Marie.L
8

votes
2

answer
762

Views

Efficiently mapping one-to-many many-to-many database to struct in Golang

Question When dealing with a one-to-many or many-to-many SQL relationship in Golang, what is the best (efficient, recommended, "Go-like") way of mapping the rows to a struct? Taking the example setup below I have tried to detail some approaches with Pros and Cons of each but was wondering what the c...
Ewan
11

votes
1

answer
152

Views

C - Creating an anonymous struct instance

In this code, a structure is defined as follows: typedef struct { int line; int column; } Pos; And later used this way: Pos get_pos ( int delta ) { ... return ( Pos ){ f->line, f->column + delta }; } The line, return ( Pos ){ f->line, f->column + delta } seems to be creating an anonymous instance of...
Jet Blue
1

votes
1

answer
542

Views

Golang Decode Nested JSON into Nested Struct

Lets look at the following code snippet: type Input struct { Value1 string Value2 string Value3 string Value4 string Nest } type Nest struct { ID string } input := &Input{} decoder := json.NewDecoder(r.Body) if err := decoder.Decode(&input); err != nil { fmt.Printf("something went...
TinuC
1

votes
1

answer
336

Views

Declaring C struct with union on C#

I want to declare _WAITCHAIN_NODE_INFO struct declaration in my code - for WCT usage. I've tried to follow the tutorials from : https://msdn.microsoft.com/en-us/library/eshywdt7(v=vs.110).aspx But every time then I use WCT call with my managed struct declaration I get heap corruption. typedef struc...
Pavel Durov
1

votes
1

answer
1.7k

Views

return array of pointers from an array in Go

Just so you know, I am quite new to Go. I have been trying to make a function like this: func PointersOf(slice []AnyType) []*AnyType{ //create an slice of pointers to the elements of the slice parameter } It is like doing &slice[idx] for all elements in the slice, but I am having trouble with how to...
Rik Schaaf
1

votes
1

answer
164

Views

What's a compact way of casting a struct into a byte slice in Go?

When writing network code we often find ourselves populating structs from byte slices to access the data in form of an object. Let's take this struct type PACKETHEAD struct { Type uint16 Size uint16 Hash uint32 } and a byte slice that has been somehow populated with data data := make([]byte, 1024) M...
I .
1

votes
3

answer
624

Views

Visualisation of the memory layout of C structs

I'm currently working on a C project that interfaces heavily with other assembly code. We're having self-made visualizations of control blocks and structs that we're using and I'm locking for a process on how to automate this process. Since we have a C struct equivalency for every assembly control b...
Frederik Hartmann
1

votes
3

answer
447

Views

Accessing a struct by index

I'm being passed a pointer to a struct, and the first 8 members are the same size, can I access them by index? typedef struct example{ uint64_t one; uint64_t two; uint64_t three; uint64_t four; uint64_t five; //etc... uint8_t ninth; } example_t; void example_method(example_t *ptr) { //can I do this?...
Anon
1

votes
1

answer
435

Views

Making a shared pointer to a new struct with initialisation

Using libc++ (GNU++11 dialect), LLVM compiler, in Xcode on a Mac. I have a struct as follows: struct Vertex { float position[3]; float colour[4]; float normal[3]; }; Currently I'm creating an instance on the heap as follows: Vertex *vertex = new Vertex({{0.1f, 0.1f, 0.1f}, {0.0f, 0.0f, 0.0f, 0.0f},...
J Bramble
1

votes
1

answer
760

Views

C array vs struct

I am currently developing a OpenGL application. So far everything worked well while programming the API. For the usual datatypes I use structs. For instance: struct vec3 { GLfloat x; GLfloat y; GLfloat z; } But now I considered to use the same datatypes as simple arrays, for instance typedef GLfloat...
Murad Babayev
2

votes
1

answer
25

Views

What is the meaning of _Space in C?

I have seen this expression in the code of other developer and I cannot get the meaning of it, the code line is: typedef struct _Space Space; So, for the syntax I reckon that _Space is a kind of variable or something similar but I do not know what kind of variable it is (integer, string, Boolean,...
Sir Skizo
1

votes
4

answer
655

Views

Python mutable NamedTuple

I am looking for a struct like data structure I can create multiple instances from and have some type hinting without being immutable. So I have something like this: class ConnectionConfig(NamedTuple): name: str url: str port: int user: str = "" pwd: str = "" client: Any = None But I would like to h...
Jodo
1

votes
2

answer
244

Views

Shared memory fork with struct and malloc

I have a problem with the shared memory between parent and child processes generated by a fork. I know how to use shared memory with primitive type as described here. While I do not know how to share struct that contains a pointer that can be allocated via malloc. For instance, suppose that I have...
erotavlas
1

votes
2

answer
34

Views

Saving input text to struct in main

got a question how to save a struct to an array and print it out in main(). My idea is, to get data of a .txt and then save it in a struct array. If i output it directly with printf() it works fine. But if I want to save it in a struct array and print it out at themain() I get trash values, nothing...
John M
1

votes
1

answer
61

Views

Trap representation for structures

I'd like to understand the trap representation concept in details. The definition is pretty clear Section 3.19.4: an object representation that need not represent a value of the object type Okay, I'd like to try it by some examples. struct test_t{ uint64_t member; }; struct test_t *test_ptr = malloc...
Some Name
1

votes
1

answer
41

Views

Adding 3 methods or a generic one [closed]

What is better, having 3 methods to get different properties: func (s *Something) GetSections() { var sections []Section // code to get sections and append to sections } func (s *Something) GetValues() { var values []Value // code to get values and append to values } func (s *Something) GetKeys() {...
Evanusso
1

votes
1

answer
52

Views

Why equality check for instance of Struct/Class are different?

I don't understand the difference between struct and class equality check. Since both Struct and Class gets their #hash from Kernel but they seem to behave differently. I know that instance.hash will produce a different result for each class instance. Struct instance has different ancestors [Custo...
MorboRe'
1

votes
2

answer
88

Views

Segmentation fault using structs in C

I'm testing out what we recently learned in class about structs and pointers by writing a small C program. However, after running it i came across a segmentation fault (core dumped) error. Can someone help me figure out where exactly that's caused? Did i miss any dangling pointers or did i do someth...
1

votes
1

answer
52

Views

How to search in array of a structure C [duplicate]

This question already has an answer here: How do I properly compare strings? 9 answers I have a problem with searching in an array of structures. Please help Here is the code I have tried so far: #include #include typedef struct{ long long unsigned num; char name[20]; }Telbook; int be(Telbook*);...
Ali Dahud
-2

votes
1

answer
18

Views

memory mangement advanced in c stored variables

I have a code example that stores variables in different places of memory: global,dynamic heap,stack and undefined. I know that all the methods are stored on the stack, all dynamically allocated memory is in the heap and all variables that are not in {} block stored on global area. I think that var...
Gilad Rozmarin
1

votes
2

answer
1.6k

Views

Sort an array of std::pair vs. struct: which one is faster?

I was wondering whether sorting an array of std::pair is faster, or an array of struct? Here are my code segments: Code #1: sorting std::pair array (by first element): #include pair client[100000]; sort(client,client+100000); Code #2: sort struct (by A): #include struct cl{ int A,B; } bool cmp(cl...
today
1

votes
1

answer
99

Views

What does the struct declaration typedef struct A { … } B; mean?

What is the name of the struct below — node_t or node? Why is there any difference? typedef struct node_t { int data; struct node_t *right, *left; } node;
noyuzi

View additional questions