• C

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.
justfunAsked:
Who is Participating?
 
rstaveleyConnect With a Mentor Commented:
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
 
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.