Solved

C structures

Posted on 2003-11-26
10
628 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 175
reading tzdatabase for timezone definitions 5 129
How to design, and implement simple interface 5 134
SQL handling single and double quotes 3 94
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

920 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

16 Experts available now in Live!

Get 1:1 Help Now