struct

Can I have an address family independent network packet struct?

I am adding IPv6 support to a network application. Some control packets should contain a mixed list of both IPv4 and IPv6 addresses (the current code only supports IPv4): struct control_packet { [...] ushort ip1_afi; struct in_addr ip1; ushort ip2_afi; struct in_addr ip2; uchar reser...

Size of two structs are the same although one of them has a struct instance in them

typedef struct BaseMessage { int u32DeviceID : 32; int u32CoreID : 32; unsigned int u16Class : 16; unsigned int u8OpCode : 8; unsigned int u16CRC : 16; } BaseMessage; typedef struct MessageWithParameters { BaseMessage base; int u8Param1 : 8; int u8Param2 : 8; } MessageWithParameters; typedef ...

What's the meaning of this C++ struct?

Hi, I got a simple C++ struct as follows: // Functor for peak to decreasing intensity sorting struct cmp_decr_int2 { bool operator() (peak2 a, peak2 b) { return a.int2 > b.int2; } }; is there an overload of the operator in this sample? ...

Defining a value of a struct in it's declaration in a header file

Please forgive me if this is a dumb question, I'm fairly new to C and couldn't find an example of this online so I assume I cant do what I want. but, hopefully someone here can point me into the right direction. so I have a headerfile that declares a struct like so typedef struct{ float *float_array1; float *float_array2; ...

How to correctly allocate memory for a structure in a function to return its value for later use and how to cast a structure into a pointer?

I'm currently self-studying C for mastering an university project. In that project we have several signatures given that we need to implement for solving our task. After several hours of trying to make some progress, I must admit that I'm totally confused about the return types of functions. I will show you some code: This is a structur...

C# Encounter a problem when convert byte[] to struct. The byte order is wrong.

I was trying to using struct to parse socket data when implement a UDP based protocol. And I searched and I can use these 2 functions to convert between byte[] and struct: byte[] StructToBytes(object structObj) { int size = Marshal.SizeOf(structObj); IntPtr buffer = Marshal.AllocHGlobal(size); try { ...

c++ structs and const

ok well i have structure struct balls { balls(UINT_PTR const &val) : BALL_ID(val){} int Ex; int Ey; const UINT_PTR BALL_ID; }; balls ball1(0x1); and i have a switch statement switch(wParam) { case ball1.BALL_ID: if(ball1.Ex<300) { ball1.Ex++; ...

How can I return a pointer to an element inside an array of structs ?

What am I doing wrong here ? /* * Consider the following pseudo code ! */ typedef struct foobar { unsigned char id, count; struct foobar *child; } foobar; foobar root = (foobar *) malloc( sizeof(struct foobar) ); root->child = (foobar *) malloc( sizeof(struct foobar) ); root->count++; root->child[0].id = 1; root->count++; r...

What does %d mean in struct.pack?

I reading though a library of python code, and I'm stumped by this statement: struct.pack( "<ii%ds"%len(value), ParameterTypes.String, len(value), value.encode("UTF8") ) I understand everything but%d, and I'm not sure why the length of value is being packed in twice. As I understand it, the structure will have little endian encoding ...

Using Struct Stat()

I'm trying to figure out how exactly to use stat() to capture information about a file. What I need is to be able to print several fields of information about a file. So.. #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> using namespace std; int main() { struct stat buf; stat("file",...

Fields in a struct skipping bytes

Hey guys, I have a struct I have written which is supposed to represent an entire UDP packet, with the ethernet header and all. Here it is: #pragma pack(1) struct UDPPacket { // an array to hold the destination mac address of the packet unsigned char dstmac[6]; // an array to hold the source mac address of the packet un...

Serialization of vectors in C++

Hello All, I'm trying to serialize a set of structs in C++. This works great for all data except a vector contained in my struct. I can write the data to disk, and then read all data back into memory. The only problem is when I try to access an element of the vector I get a segmentation fault. My code is below. Any help is greatly appre...

Struct pointer errors

int d() {return 0;} int i() {return 7;} struct a { int(*b)(); }c={d}; typedef struct e{ struct a f; }g; main() { struct e *h; h->f.b = i; } I am getting segmentation fault when I try to run this program. Can anyone justify the reason? And I also tried like int d() {return 0;} int i() {return 7;} struct a { int(*b)(); }c={d}; typ...

How can I assign values to a function pointers members in a structure

struct a { int (*ptr1)(); int (*ptr2)(); int data; }; typedef struct { struct a x; }all; int fun1() { return 5; }; int fun2() { return 9; }; I can assign like all *mem = (all*)malloc(sizeof(all)); mem->x.ptr1 = fun1; mem->x.ptr2 = fun2; Is there any other way to assign these function pointers? Is it possible to assi...

2-dimensional arrays in Objective-C?

I'm working on a basic iPhone game that requires a single-screen tilemap. Nothing difficult there. I come from a C background, so my current solution looks a bit like this: typedef struct _Tile { NSString *type; } Tile; @interface Map { Tile mapData[MAP_TILE_MAX_X][MAP_TILE_MAX_Y]; } This works fine, but I'm wondering if ther...

C: sizeof single struct member

Hi, I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ; Now I want to declare a struct child_t that has the same size as parent_t.text. How can I do this? (Pseudo-code below.) typedef st...

Serializing binary struct gcc vs cl

Full disclosure - this is homework, although completed and fully working, I'm searching for a nicer solution. I have a binary file, which was created by a program compiled within Visual Studio (I believe). The structure looks something like this. struct Record { char c; double d; time_t t; }; The size of this structure o...

C language: structs and character arrays

When I try to printf() the value of data, I get nothing, but if I were to do date = "text" It would work. Anyone know the reason for that? struct aac { char **data; }; int main ( ) { char* value = malloc ( 100 ); strcpy ( value, "test" ); struct aac b; b.data = malloc ( 100 ); cake ( value, &b ); donut ( &...

C++: Can a struct inherit from a class?

I am looking at the implementation of an API that I am using. I noticed that a struct is inheriting from a class and I paused to ponder on it... First, I didn't see in the C++ manual I studied with that a struct could inherit from another struct: struct A {}; struct B : public A {}; I guess that in such a case, struct B inherits f...

If I have three separate values that could all fit into 32 bits, does it make sense to use a uint to store them?

What I mean is, say I have a struct to represent some data and it looks like this: struct LilStruct { public readonly short A; public readonly byte B; public readonly byte C; public LilStruct(short a, byte b, byte c) { A = a; B = b; C = c; } } A short and two byte values could all fit i...