struct question

This is a discussion on struct question within the C Programming forums, part of the General Programming Boards category; I am reviewing a C program that was written in the early 90's. The program contains a usage of the ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    struct question

    I am reviewing a C program that was written in the early 90's. The program contains a usage of the STRUCT function that I have not seen before, however C is not my strong point.

    The program reads a binary data file and displays the contents on the screen. The file contains two slightly different structures, the second structure contains two additional fields.

    Code:
    struct A_1 {
    	char a
    	char b
    	long c
    	int d
    	char e
    	long v1
    	long v2
    	long v3
    	long v4
    	long v5
    	long v6
    };
    
    struct B_1 {
    	char a
    	char b
    	long c
    	int d
    	char e[6]
    	char f
    	char g
    	long v1
    	long v2
    	long v3
    	long v4
    	long v5
    	long v6
    };
    The first five fields are the same, the difference is the addition of two char fields in the second structure. The structures are defined in A.H.

    In the program A.C, amongst all the other code are the lines:

    Code:
    #include "a.h"
    
    char buffer[40];
    struct A_1 *a_1;
    struct B_1 *b_1;
    Then in main there are these two lines which I don't quite understand. I think they are doing some kind of union function??

    Code:
    a_1 = (struct A_1 *) buffer;
    b_1 = (struct B_1 *) buffer;
    Later in the code, it refers to fields in either a_1 or b_1 depending on the value of the first field (a) in the structures. So, it looks like some kind of union, but I'm not seeing it.

    Any explanations on how this works would be much appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that is essentially a "home-made union" - you have two pointers that point to the same piece of memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Using a union would probably be better.

    http://c-faq.com/strangeprob/ptralign.html

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Using a union would probably be better.

    http://c-faq.com/strangeprob/ptralign.html
    I agree. The pointer to the same bit of memory also destroys the compilers no-aliasing setting.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21