structures

Problems with structures with pointers to arrays of other structures in C

Hi there! I am attempting to tackle college worksheet on C programming (no marking for it, just to improve our learning). What we're meant to do is get a few details about shipping docks. I decided to use structures for this. My code is below, what I need help with is to print out the information (to see if its working) of whats at the...

How do I access MATLAB structure fields within a loop?

I have a structure with many fields which are vectors of different lengths. I would like to access the fields within a loop, in order. I tried getfield as follows but MATLAB doesn't like that. How can I do this? S = struct('A', [1 2], 'B',[3 4 5]); SNames = fieldnames(S); for loopIndex = 1:2 field = getfield(S, SNames(loopIndex)); ...

Python control structure utilisation

This code seems to smell: result = None for item in list: if result is None: result = item.foo(args) else: if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)): result = item.foo(args) The smelliest part is the utility of 'result'. Would anyone be kind enough sniff it f...

Multi-Root Tree Structure

I have a data such that there are many parents each with 0-n children where each child can have 0-n nodes. Each node has a unique identifier (key) Ultimately, the parents are not connected to each other. It seems like this would be a list of trees, however that seems imprecise. I was thinking of joining them with a dummy root. I need...

Initialising C structures

Is there a better way to initialise C structures? I can use initialiser lists at the variable declaration point; however this isn't that useful if all arguments are not known at compile time, or if I'm not declaring a local/global instance, eg: Legacy C Code which declares the struct, and also has API's using it typedef struct { i...

Defining a Structure in C with Malloc

I asked a question earlier on defining a structure using malloc. This was the answer I was given by the majority: struct retValue* st = malloc(sizeof(*st)); I was showing a friend my code, and we came to a stumbling block. Could someone please explain why this code works? From my viewpoint, *st hasn't been defined when you malloc it,...

Reading and Writing Structures [C]

IMPORTANT EDIT: Sorry everyone, i made a big mistake in the structure. char *name; is meant to be outside of the structure, written to the file after the structure. This way, you read the structure, find out the size of the name, then read in the string. Also explains why there is no need for a null terminator. However, i feel somewhe...

Fread binary file dynamic size string

I've been working on this assignment, where I need to read in "records" and write them to a file, and then have the ability to read/find them later. On each run of the program, the user can decide to write a new record, or read an old record (either by Name or #) The file is binary, here is its definition: typedef struct{ cha...

Why some libraries must implement basic data structure?

Some open source libraries have tendency to re implement basic structures like string, list, stack, queue... Why don't they use stl library? Is stl not good enough? ...

print the longest path between two leaf nodes

hi all, we know how to computer diameter but i came across a question where they asked you to print this longest path .. how can it be done? ...

Passing Structures by Reference? [C]

I am trying to create a linked list, with two seperate lists in one structure. That is, it holds a 'name' and a 'age' value. Then the program can either output the list sorted by name, or sorted by age. So i need two linked lists, essentially. However, i need the program to be aware of the root/head of both the name list and the age lis...

Best Data Structure For Time Series Data

Hi all, I wonder if someone could take a minute out of their day to give their two cents on my problem. I would like some suggestions on what would be the best data structure for representing, on disk, a large data set of time series data. The main priority is speed of insertion, with other priorities in decreasing order; speed of ret...

Referencing a union inside a structure using union tag gives incorrect address

I had a need to declare a union inside a structure as defined below: struct MyStruct { int m_DataType; DWORD m_DataLen; union theData { char m_Buff [_MAX_PATH]; struct MyData m_myData; } m_Data; }; Initially, I tried accessing the union data as follows (before I added the m_Data declaration):...

Why does forward declaration not work with classes?

int main() { B bb; //does not compile (neither does class B bb;) C cc; //does not compile struct t tt; //compiles class B {}; //HERE is the class B defination struct s { struct t * pt; }; //compiles struct t { struct s ...

How to resolve two structures with the same name?

In my code base I find that two modules have structures with the same name. It is giving a name conflict error. Is there a way to resolve it without changing the code? ...

How can I declare a pointer structure using {}?

This probably is one of the easiest question ever in C programming language... I have the following code: typedef struct node { int data; struct node * after; struct node * before; }node; struct node head = {10,&head,&head}; Is there a way I can make head to be *head [make it a pointer] and still have the availability to use ...

What data structure to use / data persistence

I have an app where I need one table of information with the following fields: field 1 - int or char field 2 - string (max 10 char) field 3 - string (max 20 char) field 4 - float I need the program to filter on field 1 based upon a segmented control and select a field 2 from a picker. From this data I need to look up field 4 to use ...

What is the use of declaring anonymous structures within a structure?

What is the use of defining a anonymous structure within a structure? When should this concept be used? ...

Is there an easy way to make a min heap in C++?

I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library. Thanks, ...

migrating C++ code from structures to classes

I am migrating some C++ code from structures to classes. I was using structures mainly for bit-field optimizations which I do not need any more (I am more worried about speed than saving space now). What are the general guidelines for doing this migration? I am still in the planning stage as this is a very big move affecting a major p...