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

317 вопросов

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

Как работает возврат структуры как интерфейса?

Следующий код работает, но я не могу понять, что происходит с памятью. Куда и как копируется значение структуры t? interface ITest { void Hello(); } struct STest : ITest { public void Hello() { Console.WriteLine("Hello"); } } static ITest Make() { STest t = new STest(); retu...

42
голосов
5ответов
44175 просмотров

C struct initialization using labels. It works, but how?

I found some struct initialization code yesterday that threw me for a loop. Here's an example: typedef struct { int first; int second; } TEST_STRUCT; void testFunc() { TEST_STRUCT test = { second: 2, first: 1 }; printf("test.first=%d test.second=%d\n", test.first, t...

7
голосов
2ответов
4781 просмотров

Definitions of "primitive", "value type", "struct", "class", "wrap" in Java and C#

I have been trying to understand the use of "primitives" in Java and C# and the difference between them (if any). I have asked a series of questions on SO and some of the answers seem to confuse the issue rather than clarify it. Some answers (and some MS documentation) appear to provide contradic...

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

arrays of structs need advice

I made an array of structs to represent map data that gets drawn; however I didn't double check it till it was too late: when I load in a new map I get either an "out of memory exception" (if i try to make a new array struct first) or I get a screwed up map that would require a lot of recodeing t...

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

Some questions on C++ struct and array

I am relatively new to C++ and am having problems understanding struct. I have a struct declared as follow struct MyNode { int level; int index; MyNode children[4]; } However the code fails to compile and reports error C2148: total size of array must not exceed 0x7fffffff bytes. ...

0
голосов
1ответов
1808 просмотров

Structure Generics in C#

Please, help me with this problem: I Try define a structure like this: unsafe struct sNodo<T> { public T info; public sNodo<T>* sIzq;} but i get this error: Cannot take the address of, get the size of, or declare a pointer to a managed type sNodo, how can I ...

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

Is this the right way for a block inside a struct to access a member variable in the same struct?

I'm experimenting with Obj-C blocks and trying to have a struct with two blocks in it where one block is to change what the other block does. this is a really roundabout way to do something simple... and there may be better ways to do it, but the point of the exercise is for me to understand blo...

3
голосов
8ответов
2455 просмотров

Portability concerns on C struct/union

Supposing I have the following type from an external library: union foreign_t { struct { enum enum_t an_enum; int an_int; } header; struct { double x, y; } point; }; is it safe to assume the following code fragment will work as expected on different plat...

0
голосов
4ответов
1039 просмотров

Why does Visual Studio not know the correct definition of this struct?

I've got a weird issue that almost seems like a Visual Studio 2008 issue. I have a C struct definition as follows: static struct frame { short typupdt; char callarg[1+CallSiz]; char *unitarg; XTime unitage; XTime orgtime; XTime newtime; char oldstat[1+StatSiz]; c...

1
голосов
2ответов
349 просмотров

Divide large malloc-block into smaller "partitions"

Is there a way to use poiter arithmetic on a large malloc block, so you can assign multiple structs or primitive data types to that area already allocated? I'm writing something like this but it isnt working (trying to assign 200 structs to a 15000byte malloc area): char *primDataPtr = NULL; t...

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