Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

C structures

Hello,
  I have a nested structure in C
typedef struct A
{
  uint8_t flag;
  uint8_t type;
  uint16_t len
  uint16_t val;
} pkt_a;

typedef struct B
{
         uint8_t            code;
      uint8_t            id;
      uint16_t      length;
        uint8_t         type;
        pkt_a     val
} pkt;

how can i convert the- later structure contents to a
 uint8_t [ ].The structure i guess does internal padding and has 12 byte size.. The contents are 11 bytes in size

Thanks,
Pankaj.
0
justfun
Asked:
justfun
1 Solution
 
imladrisCommented:
You can change the typedefs to be whatever you want.

Packing can be an issue, it is compiler dependant. What compiler are you using? Many compiler have switches that allow you to control the packing rules.

Or is there disk data that you are concerned about?
0
 
justfunAuthor Commented:

 I am using ANSI C compiler( gcc ) with redhat 8. I tried  using  #pragma align and pack. But the compiler ignored the directive. Its a data packet which is to be sent. now i just want to convert the structure contents to byte array.


Thanks.
0
 
DexstarCommented:
@justfun:

>  I am using ANSI C compiler( gcc ) with redhat 8. I tried  using  #pragma
> align and pack. But the compiler ignored the directive. Its a data packet
> which is to be sent. now i just want to convert the structure contents to byte array.

What was the syntax of the directive that you gave?  I think you will need to align along 1 byte boundries to get it to work.  The compiler shouldn't ignore directives like that...

Hope That Helps,
Dex*
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rstaveleyCommented:
Put #pragma pack(1) arund both structure definitions:

/* Ensure that this struct has sizeof(A) == 6 */
#pragma pack(1)
typedef struct A
{
  uint8_t flag;
  uint8_t type;
  uint16_t len
  uint16_t val;
} pkt_a;
#pragma pack()

/* Ensure that this struct has sizeof(A) == 11 */
#pragma pack(1)
typedef struct B
{
        uint8_t          code;
     uint8_t          id;
     uint16_t     length;
        uint8_t         type;
        pkt_a     val
} pkt
#pragma pack()
0
 
rstaveleyCommented:
> /* Ensure that this struct has sizeof(A) == 11 */

Oops - comment bug :-)

/* Ensure that this struct has sizeof(B) == 11 */
0
 
DexstarCommented:
rstavely:  Yeah, exactly... #pragma pack(1) should cover it.  That's why I wanted to see what code he was using.

Dex*
0
 
linuxsubCommented:
gcc does not use the #pragma  (I think). Instead, you have two options.

At compile time, you can specify -fpack-struct to pack ALL structures in your program. Something like:
gcc -c -fpack-struct program.c should do.

If you want to pack a single structure, and not pack others, define an attribute of the structure. Something like the following should do.

typedef struct B
{
        uint8_t          code;
     uint8_t          id;
     uint16_t     length;
        uint8_t         type;
        pkt_a     val
} pkt __attribute__((packed));

0
 
rstaveleyCommented:
> gcc does not use the #pragma  (I think).

It does, but #pragma is deprecated in favour of __attribute__ mechanism. See http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Pragmas.html#Pragmas

If you compile the following on GCC 2.96 or 3.2 x86, you find that either approach works:
--------8<--------
#include <stdio.h>

struct unpacked {
char c;
int i;
};

struct packed_a {
char c;
int i;
} __attribute__((packed));

#pragma pack(1)
struct packed_p {
char c;
int i;
};
#pragma pack()

int main()
{
        printf("sizeof(unpacked) is %u\n",sizeof(struct unpacked));
        printf("sizeof(packed_a) is %u\n",sizeof(struct packed_a));
        printf("sizeof(packed_p) is %u\n",sizeof(struct packed_p));
}
--------8<--------
However, only the #pragma pack mechanism works on VC7.1.

Because I toggle between VC and GCC, I favour #pragma pack, but it would be nice to have a genuinely portable approach for this. I'd be surprised if #pragma pack was ever made obsolete in GCC.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now