struct

Struct with pointer to a function

Hello, In a C struct I have defined a function pointer as follows: typedef struct _sequence_t { const int seq[3]; typedef void (* callbackPtr)(); } sequence_t; I want to initialize a var of that type globally with: sequence_t sequences[] = { { { 0, 1, 2 }, toggleArmament }, }; And I keep getting error telling me that there a...

Why do structures need to be told how big they are?

I've noticed that in c/c++ a lot of Win32 API structs need to be told how big they are. i.e someStruct.pbFormat = sizeof(SomeStruct) Why is this the case? Is it just for legacy reasons? Also any idea what "pb" stands for too? EDIT: oops, yeah I meant "cbFormat" ...

WAVEFORMATEX in a prx profile - what is the CodecData field for?

Hi All, What is the CodecData field at the end of the WAVEFORMATEX struct used for? What format is it in? How can I write an integer into it and read it somewhere else? Thanks Roey ...

how to make struct member pointer in assembly?

I`m trying to create a macro which would make easier to point to a structs member. Currently i am pointing to a structs member in assembly file using the STRUCT_NAME + offset method. For example if i want to point structs third member,i would have to do it like this: STRUCT_NAME + 3. This seems stupid way to do it, and if i insert mor...

How can a FILE* (pointer to a struct) be tested (if == NULL)?

I was playing around with C, anyways I was thinking how can file pointer (which points to a struct type), be tested if NULL as for instant: FILE *cfPtr; if ( ( cfPtr = fopen( "file.dat", "w" ) ) == NULL ) I tried to do that myself, but an error occurs. struct foo{ int x; }; struct foo bar = {0}; if (bar == NULL) puts("Yay\n...

Using C struct without including header file

My basic problem is that I want to use some structs and functions defined in a header file by not including that header file in my code. The header file is generated by a tool. Since I don't have access to the header file, I can't include it in my program. Here's a simple example of my scenario: first.h #ifndef FIRST_H_GUARD #define ...

Why freed struct in C still has data?

When I run this code: #include <stdio.h> typedef struct _Food { char name [128]; } Food; int main (int argc, char **argv) { Food *food; food = (Food*) malloc (sizeof (Food)); snprintf (food->name, 128, "%s", "Corn"); free (food); printf ("%d\n", sizeof *food); printf ("%s\n", food->name); } I still get 128 Corn...

Casting a byte array to a struct pointer depends on endianness or memory alignment?

Suppose this code: unsigned char list[3] = { 1, 2, 3 }; struct _struct{ unsigned char a; unsigned char b; unsigned char c; } *s; s = ( _struct * ) list; Can I assume that always s->a == 1, s->b == 2, s->c == 3 ? Or it will depend on the system's endianness or memory alignment? ...

Can you explicitly set a structure layout/alignment in C++ as you can in C#?

In C# you have nice alignment attributes such as this: [StructLayout(LayoutKind.Explicit)] public struct Message { [FieldOffset(0)] public int a; [FieldOffset(4)] public short b; [FieldOffset(6)] public int c; [FieldOffset(22)] //Leave some empty space just for the heck of it. public DateTime dt; } Whi...

Member access syntaxes for pointer to a structure

#include <iostream> typedef struct _person { std::string name; unsigned int age; }Person; int main() { Person *pMe = new Person; pMe->age = 10; pMe->name = "Larson"; std::cout << "Me " << (*pMe).age << " " << (*pMe).name.c_str() << std::endl; return 0; } Consider the above code. The members of the struct...

Generic constraints, where T : struct and where T : class

I would like to differentiate between following cases: A plain value type (e.g. int) A nullable value type (e.g. int?) A reference type (e.g. string) - optionally, I would not care if this mapped to (1) or (2) above I have come up with the following code, which works fine for cases (1) and (2): static void Foo<T>(T a) where T : stru...

C++ struct definition

Possible Duplicate: What does unsigned temp:3 means I just found this code in a book (was used in an example) typedef struct { unsigned int A:1; unsigned int B:1; unsigned int C:1; } Stage; What is the meaning of this structure definition? (the A:1;) ...

function takes only half of the inputs

/*-> struct sam set_of_data[4] -<*/ int main (void) { int k = 0; for(i = 0; i < 4; ++i) { char nm; double thelow, theupp; double numbers[200]; scanf("%c %lf %lf", &nm, &thelow, &theupp); for (k = 0; scanf("%lf", &numbers[k]) != 0; ++k) ; set_of_data[i] = co...

Problem Initializing an Array Of Structs

I am trying to initialize the following array of the following struct, but my code isn't compiling. Can anybody help me out? The struct/array: struct DiningCarSeat { int status; int order; int waiterNum; Lock customerLock; Condition customer; DiningCarSeat(int seatNum) { char* tempLockName; spri...

Explicit initialization of struct/class members

struct some_struct{ int a; }; some_struct n = {}; n.a will be 0 after this; I know this braces form of initialization is inherited from C and is supported for compatibility with C programs, but this only compiles with C++, not with the C compiler. I'm using Visual C++ 2005. In C this type of initialization struct some_struct n =...

Define and return a struct in c

I'm trying to convert some code from Javascript to c. The function creates an array (which always has a fixed number of items) and then returns the array. I've learned that in c it's not straightforward to return an array, so I'd like to return this as a struct instead. My c is not all that great, so I'd like to check that returning a st...

How can I have a serializable struct that wraps it's self as an int32 implicitly? in C#?

Long story short, I have a struct (see below) that contains exactly one field: private int value; I've also implemented implicit conversion operators: public static implicit operator int(Outlet val) { return val.value; } public static implicit operator Outlet(int val) { return new Outlet(val); ...

Getting the name of a Clojure struct type?

When defining a struct type and instance, I can print the value and get the "struct" implementation type: (defstruct person :name :age) (def p (struct person "peter" 30)) user=> p {:name "peter", :age 30} user=> (type p) clojure.lang.PersistentStructMap But is it possible to tell whether p is an instance of the struct type "person"? ...

Static struct in C++

Hi, I want to define an structure, where some math constants would be stored. Here what I've got now: struct consts { //salt density kg/m3 static const double gamma; }; const double consts::gamma = 2350; It works fine, but there would be more than 10 floating point constants, so I doesn't want to wrote 'static const' before ...

What does the C standard say about pointers to structs and their first member?

Consider the following two struct: struct a { int a; }; struct b { struct a a_struct; int b; }; the following instantiation of struct b: struct b b_struct; and this condition: if (&b_struct == (struct b*)&b_struct.a_struct) printf("Yes\n"); Does the C standard mandate this to always evaluate true? ...