Solved

C structures

Posted on 2003-11-26
10
630 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

821 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