Все вопросы: [struct]

277 вопросов

2
голосов
4ответов
501 просмотров

Removing Bit fields in a struct

I have a question. I have a legacy application which used bit fields in a structure. Something like this struct sample { BYTE one: 2; BYTE two : 1; BYTE three: 5; } sampletest; So three can have a value of MAX 31 only. Now I have a requirement...

22
голосов
5ответов
53994 просмотров

C initialize array within structure

I want to have an variable-length array contained within a structure, but am having trouble initializing it correctly. struct Grid { int rows; int cols; int grid[]; } int main() { struct Grid testgrid = {1, 3, {4, 5, 6}}; } Everything I try gives me an 'error: non-static initializatio...

1
голосов
5ответов
3194 просмотров

How do you define an opaque struct array in C?

I have code in my header file that looks like: typedef struct _bn bnode; I can do bnode b; just fine, but b[i], where i is an int gives me the following error: invalid use of undefined type ‘struct _bn’ Any ideas?

35
голосов
6ответов
54082 просмотров

C typedef of pointer to structure

I had come across the following code: typedef struct { double x; double y; double z; } *vector; Is this a valid type definition? The code compiles and runs fine. I was just curious if this is common practice.

0
голосов
2ответов
2017 просмотров

typedefs of structs not seeming to go through in header files?

I'm having some trouble with some struct typedef declarations in a header file not seeming to go through to my implementation file. Specifically, I have the following types defined: Type, Value, Integer, String, and Float. They are all typedef'd from struct names, in the exact same manner. I'm w...

1
голосов
3ответов
1436 просмотров

Divide a string into smaller parts & organize a structure (C-programming)

I am still learning C and I'm having some trouble figuring out how to handle this. Well, I have two structs: struct myStruct { ... struct myString *text[5]; ... } allStructs; struct myString { char part[100]; }; The objective is to have allStruct[n] point to 5 different parts ...

5
голосов
2ответов
1025 просмотров

How do I convert Perl's pack 'Nc*' format to struct.pack for Python?

I'm trying to convert a Perl script to python, and it uses quite a few different packs. I've been able to figure out the lettering differences in the "templates" for each one, but I'm having an issue with understanding how to handle Perl's lack of length declaration. example: pack('Nc*',$some_i...

4
голосов
3ответов
351 просмотров

Casting a 'BigStruct' to a 'SmallStruct' in C (similar structs with static arrays of different sizes)

Supposed that for some reason you are only allowed to use static memory in a C program. I have a basic structure that I am using in several places defined as below: #define SMALL_STUFF_MAX_SIZE 64 typedef struct { /* Various fields would go here */ ... double data[SMALL_STUFF_MAX_SI...

4
голосов
4ответов
1718 просмотров

Is it possible to force a string to be a specific size when defining a struct?

I am marshalling data between a C# and C++ application. In the C# application, I force the size of a string to be some size (say, 256 bytes). I would like to read in that exact same amount in C++ (I will be recreating the structs with reinterpret_cast) so that the data will remain formatted as ...

1
голосов
3ответов
7144 просмотров

C: copying struct/array elements

I have a file in a known format and I want to convert it to a new format, eg.: struct foo { char bar[256]; }; struct old_format { char name[128]; struct foo data[16]; }; struct new_format { int nr; char name[128]; struct...

73
голосов
6ответов
41582 просмотров

What needs to be overridden in a struct to ensure equality operates properly?

As the title says: do I need to override the == operator? how about the .Equals() method? Anything I'm missing?

0
голосов
3ответов
217 просмотров

Structs in C++ can be modified? or there is a restriction?

I have this class which has a double list template of a struct of two chars and another struct typedef struct KeyC{ char K[5]; char C[9]; } TKeyC; typedef struct Bin{ char Car; char Cad[9]; TKeyC *KC; } TBin; class Bo { private: TDoubleList<TBin> *Ent...

1
голосов
4ответов
448 просмотров

Using structs in C# for simple domain values

I am writing a financial application where the concept of 'Price' is used a lot. It's currently represented by the C# decimal type. I would like to make it more explicit and be able to change it to maybe double in the future, so I was thinking of creating a 'Price' struct that would basically act...

4
голосов
2ответов
3841 просмотров

Returning pointer to a local structure

Is it safe to return the pointer to a local struct in C? I mean is doing this struct myStruct* GetStruct() { struct myStruct *str = (struct myStruct*)malloc(sizeof(struct myStruct)); //initialize struct members here return str; } safe? Thanks.

8
голосов
7ответов
33232 просмотров

C Programming. How to deep copy a struct?

I have the following two structs where "child struct" has a "rusage struct" as an element. Then I create two structs of type "child" let's call them childA and childB How do I copy just the rusage struct from childA to childB? typedef struct{ int numb; ...

4
голосов
3ответов
24184 просмотров

Passing char array to struct member

I have the following structure: struct hashItem { char userid[8]; char name[30]; struct hashItem *next; }; In the function below I take a char pointer (char array) argument that I wish to assign to the struct. void insertItem(struct hashItem *htable[], char *userid, char *name) { ...

15
голосов
4ответов
4296 просмотров

Struct data type in Mathematica?

After playing with Mathematica's symbolic and numerical capabilities, I find it to be a decent programming language, too. However, something making it less appealing as a general-purpose language is the lack of C-like struct data type (or the record type as known in Pascal). How can I get around ...

15
голосов
3ответов
4959 просмотров

What is happening here? How can I call the default constructor when there is none?

Given the following code: public struct Foo { public Foo(int bar, int baz) : this() { Bar = bar; // Err 1, 2 Baz = baz; // Err 3 } public int Bar { get; private set; } public int Baz { get; private set; } } What does : this() actually do? There is no defaul...

-2
голосов
2ответов
691 просмотров

Xaml serialization and immutable structs?

How can I do this? Tried using a TypeConverter, but the only thing I could think of was to construct the XML for the types, which doesn't quite cut it. TypeConverters in xaml serialization will escape xml and treat it like plain text. Value converters aren't much better. Now, I'm moving to IS...

2
голосов
3ответов
5018 просмотров

Handling input into struct elements with array

I'm doing an assignment that involves structs. You are to make a struct with three variables and declare 3 instances of that struct. The program will then ask for the user to input 9 pieces of information. I don't like having to type cout and cin unnecessarily (9 sequential times?), so I was thi...

3
голосов
11ответов
479 просмотров

Need way to alter common fields in different structs

I'm programming in C here, for Windows and various Unix platforms. I have a set of structs that have common fields, but also fields that are different. For example: typedef struct { char street[10]; char city[10]; char lat[10]; char long[10]; } ADDR_A; typedef struct { char...

7
голосов
3ответов
1746 просмотров

Blindly converting structs to classes to hide the default constructor?

I read all the questions related to this topic, and they all give reasons why a default constructor on a struct is not available in C#, but I have not yet found anyone who suggests a general course of action when confronted with this situation. The obvious solution is to simply convert the struc...

2
голосов
2ответов
374 просмотров

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

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 seria...

1
голосов
3ответов
186 просмотров

Giving an instance of a class a pointer to a struct

I am trying to get SSE functionality in my vector class (I've rewritten it three times so far. :\) and I'm doing the following: #ifndef _POINT_FINAL_H_ #define _POINT_FINAL_H_ #include "math.h" namespace Vector3D { #define SSE_VERSION 3 #if SSE_VERSION >= 2 #include <emmintrin.h&g...

1
голосов
6ответов
566 просмотров

a few beginner C questions

I'm sort of learning C, I'm not a beginner to programming though, I "know" Java and python, and by the way I'm on a mac (leopard). Firstly, 1: could someone explain when to use a pointer and when not to? 2: char *fun = malloc(sizeof(char) * 4); or char fun[4]; or char *fun = "fun"; And...

2
голосов
2ответов
581 просмотров

C++ Stack Implementation

Hey all! Having a little trouble with my stack. Im trying to print each element that I've pushed onto the stack. Starting with the stack ctor we know that we have a fixed size for the array. So I allocate the items struct object to hold just that much space: stack::stack(int capacity) { it...

0
голосов
3ответов
246 просмотров

Problem while porting VB.NET Code to C#

Currently I am trying to port some VB.NET code to C#. The struct looks like this in VB.NET: Public Structure sPos Dim x, y, z As Single Function getSectorY() As Single Return Math.Floor(y / 192 + 92) End Function Function getSectorX() As Single Return Math.Floor(...

1
голосов
1ответов
125 просмотров

Read double fields from structure

I´m not very experienced with C# but I urgently have to read a variable from a structure given in a library. this is the definition of the structure in the library: public struct mtLocation { public bool bAltitudeValid; public bool bCoordinatesValid; public double dAltitude; p...

0
голосов
3ответов
1134 просмотров

C++ Stack by Array Implementation

What I want to happen is for the pushFront(int) function to do this: bool stack::pushFront( const int n ) { items[++top] = n; // where top is the top of the stack return true; // only return true when the push is successful } items is a struct type of the object "item". Have a look: cl...

0
голосов
2ответов
2613 просмотров

Understanding XML-RPC param possibilities, especially recursion of values

One thing I've noticed with all the XML-RPC examples out there, including the spec itself, is there is no detailed example of a schema using recursive (param) values. It is hard to understand what should actually be possible within XML-RPC without these illustrations, and I wonder if someone coul...