Actually few hours back, I had posted a question but there was some error in my question (My bad!!). I want to access the members of a structure (profile_t) which is placed inside a structure (profile_datagram_t) and profile_t is a array of structs....
struct profile_t
{
unsigned char length;
unsigned char type;
unsigned char *data;
};
typedef struct profile_datagram_t
{
unsigned char src[4];
unsigned char dst[4];
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;
This sets the length of the first profile in the headers to 10:
header h;
h.profiles[0].length = 10;
Related
Good moorning,
I am trying to create a struct of date of this style:
//Struct 1
union{
struct{
union{
struct{
bool interrupt_receive;
bool fifo_mode;
bool enable;
bool bits_14_mode;
bool int_source_readed;
bool void6;
bool void7;
bool void8;
};
unsigned char registro;
}REG_CTRL_FLG;
union{
struct{
unsigned int REG_CTRL_TIMEH;
unsigned int REG_CTRL_TIMEL;
};
unsigned short REG_CTRL_TIME;
};
unsigned int threshold; //Para pruebas pendiente de eliminar
//AXIS_STRUCT data_axis;
//AXIS_EXTEND_STRUCT data_full_axis;
}registros_acelerometro;
unsigned int registro[3];
}ACELEROMETRO;
//Struct 2
typedef union{
struct{
unsigned char dlc_0;
union{
struct{
unsigned char p15:1;
unsigned char p14:1;
unsigned char p13:1;
unsigned char p12:1;
unsigned char p11:1;
unsigned char p10:1;
unsigned char p9:1;
unsigned char p8:1;
unsigned char p7:1;
unsigned char p6:1;
unsigned char p5:1;
unsigned char p4:1;
unsigned char p3:1;
unsigned char p2:1;
unsigned char p1:1;
unsigned char p0:1;
};
unsigned short p;
}pulsadores;
union{
struct{
unsigned char lb0:1;
unsigned char lb1:1;
unsigned char ls0:1;
unsigned char ls1:1;
unsigned char iluminacion:1;
unsigned char completo:1;
unsigned char sobrecarga:1;
unsigned char void7:1;
}registro_cabina;
unsigned char estado_cabina;
};
unsigned char dlc_4;
}registros_can_data;
unsigned char can_data[5];
}TRESA_PULS;
The struct 1, i have been used with a STM32F7, ARM Compiler 6.11, and a Keil MDK 5.21, without any problem, but with the configuration that i use in the struct 2, it causes me some problems
The unions that contained int the structure, seem need a aditional memory space to work, which distorts the structure.
I've been thinking about it, but i dont find the error
Do you know if anything has changed?
Is there a pre-processing order that should be used?
Has it happened to any of you?
Do you see any trouble?
Thanks for your help and time.
Kinds Regards.
This is simple code; I am using this code snippet in my project and its working fine.
My question if it is the best way or otherwise What is the best way to set the bit fields using loops in C?
#define NUM_OF_MAX_SENSOR 10
typedef float SENSOR_VAL ;
typedef struct
{
unsigned char bit0:1;
unsigned char bit1:1;
unsigned char bit2:1;
unsigned char bit3:1;
unsigned char bit4:1;
unsigned char bit5:1;
unsigned char bit6:1;
unsigned char bit7:1;
}BYTE_FIELD;
typedef struct
{
BYTE_FIELD funP1;
BYTE_FIELD funP2;
}LOG_DATA;
LOG_DATA xlog;
typedef struct
{
unsigned char ok:1;
unsigned char open:1;
unsigned char shrt:1;
unsigned char unused:5;
}SENSOR_FLAGS;
typedef struct
{
SENSOR_FLAGS flag;
SENSOR_VAL val;
}SENSOR_DATA;
typedef union
{
SENSOR_DATA sensor[NUM_OF_MAX_SENSOR];
unsigned char buff[sizeof(SENSOR_DATA) * NUM_OF_MAX_SENSOR];
}SENSOR;
SENSOR sensor;
These are the structures I used and these are the max details I can provide.
void UpdateLogData()
{
int i = 0;
unsigned char *funPAddr =(unsigned char *) &xlog.funP1;
for(i=0;i<8;i++)
*(funPAddr) |= sensor.sensor[i].flag.ok << i;
printf("0x%x",*(funPAddr));
}
//main
int main()
{
sensor.sensor[0].flag.ok = 1;
sensor.sensor[4].flag.ok = 1;
sensor.sensor[5].flag.ok = 1;
sensor.sensor[6].flag.ok = 1;
sensor.sensor[7].flag.ok = 1;
UpdateLogData();
}
i'm trying to find the most efficient way to use the 'free' command in a program I made. Basically there are several structers, the first is called Operation. Here's how it's defined -
struct Operation {
unsigned int ic;
unsigned int dc;
struct symbolStruct *externHead;
struct symbolStruct *symbolHead;
struct DataCode *dataHead;
struct MachineCode *machineHead;
int linenumber;
};
It has several pointers to other structers, let's take machineHead for example.
struct MachineCode {
unsigned int row : WORD_SIZE;
unsigned int line;
OperandType *structure;
struct MachineCode *next;
};
And OperandType looks like this -
typedef struct {
unsigned int numOfOperands : 2;
unsigned int addrMethSou : 2;
unsigned int addrMethDest : 2;
unsigned int operation : 4;
unsigned int extraWords : 2;
char *firstOperand;
char *secondOperand;
} OperandType;
What I want to do is to free the strings "firstOperand" and "secondOperand" in structure (which is in machineHead ) and then to free machineHead itself, I tried to write it down using the following code -
void clear(struct Operation *op) {
struct MachineCode *mh = op->machineHead, *fmh;
while(mh != NULL) {
fmh = mh;
mh = mh->next;
free(fmh->structure->firstOperand);
free(fmh->structure->secondOperand);
free(fmh->structure);
free(fmh);
}
But the program crashes in runtime. Is there an elegant way to do it or do I have to make a pointer varibale of every type in order to clear the memory?
I want to define a structure that contains a data array and some pointers to the same data array. So I defined the structure and then I initialize the pointer like this:
typedef struct {
unsigned char data[MAX_PACKET_DATA];
unsigned char* sector1;
unsigned char* sector2;
unsigned char* sector3;
} Packet;
[...]
NGSCTransmittingDataPacket packet
[...]
packet->sector1 = packet.data + SECTOR1_OFFSET;
packet->sector2 = packet.data + SECTOR2_OFFSET;
packet->sector2 = packet.data + SECTOR3_OFFSET;
Can I initialize the pointer directly inside the typedef struct definition? If I write
typedef struct {
unsigned char data[MAX_PACKET_DATA];
unsigned char* sector1 = data + SECTOR1_OFFSET;
unsigned char* sector2 = data + SECTOR2_OFFSET;
unsigned char* sector3 = data + SECTOR3_OFFSET;
} Packet;
the compiler gives me error.
Any solution?
you cannot do that, C won't allow it. But in your example you could do that equivalent:
typedef struct {
unsigned char data[SECTOR1_OFFSET];
unsigned char sector1[SECTOR2_OFFSET-SECTOR1_OFFSET];
unsigned char sector2[SECTOR3_OFFSET-SECTOR2_OFFSET];
unsigned char sector3[MAX_PACKET_DATA-SECTOR3_OFFSET];
} Packet;
There's no padding since all members are char arrays.
You normally cannot go further SECTOR1_OFFSET-1 for an index of data, but there it would work (or create an union with a MAX_PACKET_DATA-length array if you want to make it cleaner, because some compilers could complain if you're accessing data with a bigger index)
Example with an union of 2 anonymous structures:
#include <stdio.h>
#define SECTOR1_OFFSET 20
#define SECTOR2_OFFSET 50
#define SECTOR3_OFFSET 80
#define MAX_PACKET_DATA 100
typedef union
{
struct
{
unsigned char sector0[SECTOR1_OFFSET];
unsigned char sector1[SECTOR2_OFFSET-SECTOR1_OFFSET];
unsigned char sector2[SECTOR3_OFFSET-SECTOR2_OFFSET];
unsigned char sector3[MAX_PACKET_DATA-SECTOR3_OFFSET];
};
struct
{
unsigned char data[MAX_PACKET_DATA];
};
} Packet;
int main()
{
Packet p;
p.data[SECTOR3_OFFSET] = 'a';
p.data[SECTOR3_OFFSET+1] = 'z';
p.data[SECTOR3_OFFSET+2] = '\0';
printf("sector 3 %s\n",p.sector3);
return 0;
}
result:
sector 3 az
I am creating an application which uses a vCard struct. Currently, this struct looks like this:
typedef struct {
char *version;
char **names;
char *formatted_name;
char *nickname;
char *organisation;
char *title;
struct { /* Emails */
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} *emails;
struct { /* Phones */
char *type;
char *number;
unsigned preferred : 1;
} *phones;
struct { /* Addresses */
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} *addresses;
time_t birthday;
struct { /* Custom Fields */
char *field_name;
union {
/* Single value */
int i;
float f;
double d;
time_t t;
struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} email;
struct {
char *type;
char *number;
unsigned preferred : 1;
} phone;
struct {
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} address;
char *s;
/* Multiple values */
int *is;
float *fs;
double *ds;
time_t *ts;
struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} *emails;
struct {
char *type;
char *number;
unsigned preferred : 1;
} *phones;
struct {
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} *addresses;
char **ss;
} field_value;
} *custom_fields;
} vCard;
This one is huge and takes much memory. I also use much pointers. Is there a better and cleaner way to declare this struct? Thanks.
Also, is it a good practise to use unions inside of structs and vice versa?
Yes! Split structs apart! (just like when there's a huge function.)
typedef struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} vCardEmail;
...
typedef struct { /* Custom Fields */
char *field_name;
union {
/* Single value */
int i;
float f;
double d;
time_t t;
vCardEmail email;
vCardPhone phone;
vCardAddress address;
char *s;
/* Multiple values */
int *is;
float *fs;
double *ds;
time_t *ts;
vCardEmail *emails;
vCardPhone *phones;
vCardAddress *addresses;
char **ss;
} field_value;
} vCardCustomField;
typedef struct {
char *version;
char **names;
char *formatted_name;
char *nickname;
char *organisation;
char *title;
vCardEmail *emails;
vCardPhone *phones;
vCardAddress *addresses;
time_t birthday;
vCardCustomField *custom_fields;
} vCard;