struct

Reshaping nested struct arrays to cell array having elements with different sizes

一曲冷凌霜 提交于 2020-01-04 02:19:05
问题 I have a similar question to my previous one. This time the form of the nested structure looks like this: Sizes = [2, 5, 8, 6, 3]; cells = 5; for i = 1:cells for j = 1:Sizes(i) a(i).b.c(j).d = rand(1,1); end a(i).b.Size = Sizes(i); end Again I would like to put all the d values of a(:).b.c(:) into a single cell array that contains 1 x cells cells. Here is my solution using cellfun but I would like to avoid this function: ab = [a.b]; abc = {ab.c}; abcd = cellfun(@(x) [x.d], abc, 'UniformOutput

Does memcpy preserve data between different types?

ⅰ亾dé卋堺 提交于 2020-01-04 01:55:31
问题 Does calling memcpy on two different structures preserve the original data if the buffer size is sufficient? And is it defined to retrieve values of another data type with data of previous data type if their respective data types overlap? This should be similar for both c/cpp languages but I'm providing an example in cpp - #include <iostream> #include <cstring> using namespace std; struct A{ int a; char b[10]; }; struct B{ int ba; int bb; }; int main(){ B tmp; tmp.ba = 50; tmp.bb = 24; cout <

Opaque structures with multiple definitions

北城以北 提交于 2020-01-04 01:26:07
问题 I am considering realizing a simple interface pattern in the C language. A key characteristic is that it would provide multiple definitions for an opaque sturcture supplied by the interface's public header, that is, different implementations would provide different underlaying data for that structure (so across different translation units, the same structure would have different implementation). I couldn't find any reference whether this would be a good or bad design pattern. At least it

How do I apply a structure offset?

醉酒当歌 提交于 2020-01-03 18:43:12
问题 I have a structure typedef struct foo { int lengthOfArray1; int lengthOfArray2; int* array1; int* array2; } foo; I need to allocate enough memory for the entire structure and its array's contents. So assuming each array had a length of 5... foo* bar = (foo*)malloc(sizeof(foo) + (sizeof(int) * 5) + (sizeof(int) * 5)); I now have to point array1 and array2 to the correct location in that allocated buffer: bar->array1 = (int*)(&bar->lengthOfArray2 + sizeof(int)); bar->array2 = (int*)(bar->array1

What are the benefits of an immutable struct over a mutable one?

喜欢而已 提交于 2020-01-03 17:01:04
问题 I already know the benefit of immutability over mutability in being able to reason about code and introducing less bugs, especially in multithreaded code. In creating structs, though, I cannot see any benefit over creating a completely immutable struct over a mutable one. Let's have as an example of a struct that keeps some score: struct ScoreKeeper { var score: Int } In this structure I can change the value of score on an existing struct variable var scoreKeeper = ScoreKeeper(score: 0)

Why Java doesn't support structures ? (Just out of curiosity)

天涯浪子 提交于 2020-01-03 12:34:02
问题 I know you can use public fields, or some other workarounds. Or maybe you don't need them at all. But just out of curiosity why Sun leave structures out. 回答1: Although Java can support arbitrarily many kinds of classes, the Runtime only supports a few variable types: int, long, float, double, and reference; additionally, the Runtime only recognizes a few object types: byte[], char[], short[], int[], long[], float[], double[], reference[], and non-array object. The system will record a class

Why Java doesn't support structures ? (Just out of curiosity)

别等时光非礼了梦想. 提交于 2020-01-03 12:33:25
问题 I know you can use public fields, or some other workarounds. Or maybe you don't need them at all. But just out of curiosity why Sun leave structures out. 回答1: Although Java can support arbitrarily many kinds of classes, the Runtime only supports a few variable types: int, long, float, double, and reference; additionally, the Runtime only recognizes a few object types: byte[], char[], short[], int[], long[], float[], double[], reference[], and non-array object. The system will record a class

Python: How to pack different types of data into a string buffer using struct.pack_into

霸气de小男生 提交于 2020-01-03 08:24:06
问题 I'm trying to pack some unsigned int data into a string buffer created using ctypes.create_string_buffer . Here is the following code segment, and a running example showing the error on codepad: import struct import ctypes import binascii buf = ctypes.create_string_buffer(16) struct.pack_into("=I=I=I", buf, 0, 1, 2, 3) print binascii.hexlify(buf) This yields the following error: ... struct.error: bad char in struct format The documentation doesn't allude to whether you can pack data of

passing different structs to a function(using void *)

戏子无情 提交于 2020-01-03 05:49:06
问题 I need to figure out how to pass two different structs to a function. I tried using void * for the parameter but i am receiving the error: warning: dereferencing 'void *' pointer error: request for member left in something not a structure or union same error for member right here is what i did in general terms(code may not compile). struct A{ char *a; struct A *left, *right; } *rootA; struct B{ char *b; struct B *left, *right; } *rootB; void BinaryTree(void *root, void *s){ if(condition) root

function pointer incompatible pointer types void (*)(void *) from void (my_type *)

一个人想着一个人 提交于 2020-01-03 05:46:04
问题 I'm having getting the above warning and understand it, but just don't know how to fix it. My code is below, but basically what I'm doing is storing a function pointer in a struct, and initializing the struct in another function that I call from my main. The code seems to compile when I use it with a default function (i.e. free()) but not when I use it with the test_func function below. Thoughts? Struct: typedef struct my_struct { my_type **double_pointed; int num; void (*funcp)(void *data);