Solved

C structures

Posted on 2003-11-26
10
627 Views
Last Modified: 2010-07-27
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
Comment
Question by:justfun
10 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9828190
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
 

Author Comment

by:justfun
ID: 9828227

 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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9828382
@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
 
LVL 17

Accepted Solution

by:
rstaveley earned 75 total points
ID: 9830544
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 17

Expert Comment

by:rstaveley
ID: 9830549
> /* Ensure that this struct has sizeof(A) == 11 */

Oops - comment bug :-)

/* Ensure that this struct has sizeof(B) == 11 */
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9836797
rstavely:  Yeah, exactly... #pragma pack(1) should cover it.  That's why I wanted to see what code he was using.

Dex*
0
 

Expert Comment

by:linuxsub
ID: 9918655
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9919367
> 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now