Questions tagged [struct]

1

votes
1

answer
1.3k

Views

access golang struct field with variable

For reference: example here I am trying to access a struct field using a variable key, to explain in PHP you can do the below. $arr = ['test' => 'foo']; $key = 'test'; $result = $arr[$key]; Or in Javascript you can do the below const obj = {'test': 'foo'} const key = 'test' const result = obj[key] I...
Sam White
1

votes
1

answer
60

Views

How do I use a mixed type c++ struct and only pass some values to OpenGL?

I have read high and low and thought I understood C++ and OpenGL vertex data layouts, but I must be wrong somewhere... I have a struct to create a Line object. Therefore it has two points (each of 3 floats to represent a vector position). It must also have an object ID to allow me to track the speci...
Nas25
1

votes
2

answer
36

Views

Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct'

template class Vec { T data[S]; public: constexpr Vec(const T& s) : data{s} {} }; template class Mat { Vec data[Rows]; public: constexpr Mat(const T& s) : data{Vec(s)} {} }; int main() { constexpr Mat m{1.0}; return 0; } This code gives me the following error: source/main.cpp:24:25: error: could n...
David Bermejo
1

votes
2

answer
26

Views

Having Trouble Populating an Array of Structures from Reading a File

I'm trying to populate an array with values from this file: 9383 8.86 2777 69.15 7793 83.35 5386 4.92 6649 14.21 2362 0.27 8690 0.59 7763 39.26 540 34.26 9172 57.36 5211 53.68 2567 64.29 5782 15.30 2862 51.23 4067 31.35 3929 98.02 4...
yiwll
1

votes
2

answer
51

Views

How to omit some parameters of structure Gin gonic

I have big structure with more than 50 params type Application struct { Id int64 `json:'id'` FullName string `json:'fullName,omitempty'` ActualAddress string `json:'actualAddress,omitempty'` ..... } I use gin-gonic and when I return application I need to omit so...
mondayguy
1

votes
1

answer
15

Views

Definition of 2D array inside struct in RPC Specification not working

I am trying to implements an NFS using RPC. Now my specification file looks something like this: (This is very a basic version of it :) ) struct input { char command[20]; char arg[10][10]; int numargs; }; struct lsresult { char arr[50][256]; }; program NFSPROG { version NFSVERSION { lsresult ls(...
Ricky
1

votes
2

answer
36

Views

After sending a pointer to an external function to make it null, it doesn't change to null [duplicate]

This question already has an answer here: Passing by reference in C 17 answers I have created a pointer of type 'Vector' (see code). After creation I want to send a pointer of type Vector (meaning Vector*) to a function called 'VectorDestroy(Vector* _vector), which frees the struct from memory and...
Majd Kharman
1

votes
3

answer
467

Views

structs and arrays

I am currently looking at this: some Genetic Algorithm This is some adapted code: struct Chromosome { public bool[] genes; public int fitness; } I have never used structs in my evolutionary algorithms/genetic algorithms. Is it not a bit pointless to use arrays in structs - especially when I have to...
cs0815
0

votes
1

answer
22

Views

C++ - passing declared array as argument to struct

float_t rotations[6] = {1.0f, 2.5f, 3.0f, 4.0f, 5.0f, 6.0f}; fla_algo_config_t config_alt = {20.0f, 20.0f, {1.0f, 2.5f, 3.0f, 4.0f, 5.0f, 6.0f}, mock_error_callback, nullptr}; fla_algo_config_t config = {20.0f, 20.0f, rotations, mock_error_callback, nullptr}; config_alt works. However, it is cumbers...
1

votes
1

answer
60

Views

Segmentation fault (core dumped) when using my own struct with pointers to array

Hello can you tell me why is the compiler building sucessfully when there's only one largenumber a, but when I declare another largenumber b and do the exact same things it shows Segmentation fault (core dumped) . I don't understand what's wrong? I can't declare other of type largenumber because it...
Adrian Zyskowski
1

votes
4

answer
68

Views

How do I retrieve a value from struct object by previously stored ID? C++

I have a C++ script with a struct object 'Particle' , with x, y ,z position stored. I create several particle objects with unique ID's. How do I get the position of a particle by ID? Something like: particle.getPosByID(int ID); My code is as follows: struct Particle { Vector position { 0.0f, 0.0...
Nas25
1

votes
2

answer
60

Views

Nested struct access in C

I can't figure out why my code causes a segfault when I try the following give these structs: typedef struct Vertex { int i; int color; } vertex; typedef struct Edge { vertex v1; vertex v2; } edge; typedef struct Node { vertex *v; struct Node *next; } node; node *nodehead = NULL; code causing the is...
naike
1

votes
1

answer
114

Views

How to decode JSON that has multiple lists that are unnamed in Swift [duplicate]

This question already has an answer here: Decoding JSON array of different types in Swift 3 answers I am trying to decode a JSON string but it seems to have multiple lists that do not have any names/keys that I can call with my structs. From what I can tell (with the little knowledge I have) there...
Jack Welch
1

votes
5

answer
64

Views

Nested struct or class stored in a list in c#

is it possible to store nested structs or classes in lists using c#? looking at the following code segments. Nested Scruct: struct structBooks { public string strBookName; public string strAuthor; public structPubished publishedDate; } struct structPubished { public int intDayOfMonth; public int int...
Sara Payne
1

votes
1

answer
32

Views

How to use members of a structure for arithmetic operators (add,sub,mul,div a variable to a struct member)

Sorry for the noob question. How do i use arith operators to variables within the structure? I have this struct struct account { int no; char name[100]; int pin; float id; }; what I want to do is add a value to float id #include #include #include #include main() { struct account { int no; char...
marc jose
1

votes
2

answer
58

Views

Struct inside union, uart status

I am using MPLABX IDE MCC generated code. At some point inside UART driver, there is the following code for UART state. typedef union { struct { unsigned perr : 1; unsigned ferr : 1; unsigned oerr : 1; unsigned reserved : 5; }; uint8_t status; }uart1_status_t; As far as I can understand there are 2...
Arda30
1

votes
1

answer
63

Views

Why does a struct's field gets reset after the using statement?

I noticed some weird behavior with IDisposable structs. The dispose method seems to be called on a new instance with the fields set to default values. public static class Example { public static void Main() { var data = new MyStruct(); using (data) { data.Foo = 'some string'; Console.WriteLine(data....
john doe
1

votes
4

answer
2.7k

Views

How to print three bytes in a struct in C

I have the following C code: person->title The title is a three byte char array. All I want to do is print those three bytes. I can't use the string formatter with printf because there is no null byte. How can I print this out without having to copy it or add a null byte to it? Thanks in advance! UP...
PeterM
1

votes
2

answer
667

Views

with tuples and boost.fusion, is there any reason to use struct?

tuples are similar to structs (discussed here: Boost::Tuples vs Structs for return values). it seems that the only advantage is it's more convenient to write struct because it's shorter and by key rather than a number. one can also use fusion map and structs to access by key Boost::Tuples vs Structs...
kirill_igum
3

votes
1

answer
31

Views

How do I print a string from a struct?

So I have the string john. I pack it into a struct. When I unpack it, how can I print john? Currently it only prints j. Same thing if I changed the string to Sammy or other names with different lengths? I have 2 functions to pack and unpack the struct. This what I don't need to worry about the lengt...
Mary
1

votes
3

answer
398

Views

C++ struct bit field doesn't parse data correctly

I'm trying to extract fields from a VLAN Header using a packed struct: I created this struct: #pragma pack(push, 1) struct vlan_header { uint16_t PCP : 3, DEI : 1, ID : 12; }; #pragma pack(pop) When I take an uint8_t array and try to extract the fields from it: uint8_t* data; vlan_header* vlanHeader...
Lior Sharon
1

votes
0

answer
48

Views

Initializing an array of structs when no explicit constructor is provided

If I have a struct: struct S { int x = 0, y = 0; }; Then, is it OK to initialize an array of such structs like this? S s[5] = {}; so that x and y in each struct is 0? So, do x and y become a part of the implicit constructor? And if yes, do I need the = {}; then at all?
user3496846
1

votes
0

answer
79

Views

Write struct to text file gives Korean letters

The program I'm writing is suppose to ask the user for name, haircolor, age and weight and these are then placed in a struct. The struct is then written to a file 'REGISTER.TXT' among other heroes. I'm using three separate files of main.cpp, hero.h and hero.cpp where the latter holds the current fun...
R.Englund
1

votes
4

answer
58

Views

geting each element of a textfile until ':' and store on struct variable

I need help :/ On textfile, I have something like this: MyName: 15 3 and I want to read each element until ':' and store it in variable name of the struct, and read the next elements and store 15 and 3 in another variables. typedef struct STRUCT_PLAYER{char name[20]; int sucess; int numQuest; int...
Ricardo
1

votes
1

answer
54

Views

Extract variable names and offsets from a compound C struct

I am working on a Wireshark plugin that must decode a large packet that is created by writing a struct into the packet. If I am able to know the variable names and offset of the original structure, I would then be able to decode the data without regard to the original compiler. The struct is large...
alindber
1

votes
3

answer
525

Views

swift: Listen for “struct variable”-change event?

I got a small problem : How is it possible to listen for the change of a struct-instance variable declared in another ('uneditable!!') class? I added some small code snippets to maybe clarify my thoughts. instructions: FixedClass is a uneditable class: I don't want to change / I'm not able to chan...
Jonas0000
1

votes
1

answer
40

Views

Accessing an integer within a structure which has Pointer to structure

I am new to pointers and trying to use a pointer to the structure.I need to access the structure parameter using pointer. Kindly assist me. Here is the program: #include typedef struct{ int a; int b; int c; }time; typedef struct{ int a; int b; time record; }myarray; typedef struct{ myarray *ptrtoarr...
Gopala Krishna
1

votes
2

answer
157

Views

Swift: Undeclared Variable with Multiple Type Options

I'm learning swift through Apple's App Development book. There is a project for designing a Favorite Athlete App to view, add and edit athletes. I'm wondering if there is a way to have a variable in a struct/class that is of a type that is dependent on the literal of another variable.... Ok, so this...
Average_Pickle
1

votes
1

answer
70

Views

Struct array data in the first index is reset for some ATmega 2560

I have 3 exactly same PCB's accommodating ATmega2560 MCU. I'm using Atmel Studio 7 for fuses and flashing the HEX. I'm using Visual Studio with Visual Micro plug-in using a mix of C and Arduino coding. I have a struct array definition. I populate data into this struct array from a json fed by Seria...
Sener
1

votes
1

answer
323

Views

“installing” Kaitai Struct Python

I need help with installing Kaitai Struct on my Laptop. I installed python-kaitaistruct and compiled the network files. But i get an import error: Traceback (most recent call last): File 'test2.py', line 1, in from ethernet_frame import * File '/home/bene/python/ethernet_frame.py', line 15, in fro...
Benedict Schlüter
1

votes
1

answer
39

Views

Using ctypes to overwrite an accessable struct from a library

I want to use a C-library in Python, I use ctypes to do that. I defined the struct that I want to access in the library, I want to create multiple instances of these structs in my Python program and overwrite the struct in the C-library with one of the instances, let it calculate and then overwrite...
Patrick
1

votes
2

answer
201

Views

How to handle member padding in C struct when reading cffi.buffer with numpy.frombuffer?

I have to read an array of C structs returned from a dll and convert it to a Numpy array. The code uses Python's cffi module. The code works so far but I don't know how to handle the member padding in the struct that np.frombuffer complains about: ValueError: buffer size must be a multiple of elemen...
Joe
1

votes
1

answer
49

Views

const pointer to const inside srtuct in function prototype

I have the following struct: struct foo { ... char* cp; }; And I want to pass a struct foo type pointer to a function, but I want the function to cast the pointer to const char* const cp, and I don't want the const qualifier as part of the definition of struct foo. Declaring the function as: void fu...
Aayla Secura
1

votes
0

answer
38

Views

Python3.4 packing and format type with struct

I have a binary file which I am trying to recreate by writing data to a new file. The data in it should be stored as 32-bit (4-byte) twos- complement binary integers or as ASCII characters. The first 4 bytes should represent the integer 0, then the next 4 should be the integer 4, the next 4 should b...
wxman73
1

votes
2

answer
659

Views

How to read data in a file and create a vector of struct?

I tried to read a file called 'qbdata.txt' and save the data in a vector called quarterbacks. So, I created this struct 'Record' to save different types of variables in the file and 'quarterbacks' is supposed to be a vector of struct. Here is my code. However, it didn't work out. When I test the siz...
Phyllis Qu
1

votes
1

answer
478

Views

Unmarshal a YAML to a struct with unexpected fields in Go

I encountered an issue while trying to unmarshal a struct with an unexported field using github.com/go-yaml/yaml. The struct looks like: type Example struct { ExportedField string `yaml:'exported-field'` OneMoreExported string `yaml:'one-more-exported'` unex...
xaxes
1

votes
0

answer
205

Views

Declare lifetime of output struct to be unrelated to inputs [duplicate]

This question already has an answer here: Return local String as a slice (&str) 3 answers Is there any way to return a reference to a variable created in a function? 2 answers I have defined two structs and a function in my Rust program, in which I am implementing my own linear algebra. While doin...
spyduck
1

votes
2

answer
110

Views

getattr on a ctypes structure returns a handle and not a value

The below code snippet seems to be returning a handle instead of a value while getting the c_int attribute of my ctypes structure. How can the method get_val be changed to return the value of either o_num or o_str? import ctypes class my_struct(ctypes.Structure): _fields_=[('o_num',ctypes.c_int),('o...
RogerLePatissier
1

votes
2

answer
337

Views

How to update an array with WritableKeyPath in Swift 4.0

In Swift 4.0, I have an array of structs. Is there a way to use keyPaths to update all items in the array without using manually iterating like map or forEach? Something similar to objc [people makeObjectsPerformSelector: @selector(setName:) withObject: @'updated']; struct Person { var name: String?...
joels
1

votes
0

answer
285

Views

Switching endianness in the middle of a struct.unpack format string

I have a bunch of binary data (the contents of a video game save-file, as it happens) where a part of the data contains both little-endian and big-endian integer values. Naively, without reading much of the docs, I tried to unpack it this way... struct.unpack( '3sB
Quuxplusone

View additional questions