Thread: Compile time switch

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Compile time switch

    Hello,

    I have two applications sharing same .h file.

    Now I want to change a structure for only one application.

    So can I do like this?


    Code:
    struct devrqstPtr
    {
         UBYTE devReqPointer;
         UWORD frequency;
    #ifdef HOST
        UBYTE   Status;
        UWORD RespLength;
        UBYTE   response[256];
    #endif
    };
    And # define HOST in .c file of one application.

    Please let me know,,

    Thanks,

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So can I do like this?
    Try it and see.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    It works

    RT

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It works
    Experimentation? Yes, indeed.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Declaring arrary of pointer, pointing to array of structure.

    Hello All,

    Here is what I am trying to do.

    Following code is present in .h file.

    Code:
    struct _tagdevRequestPointer
    {
    	UBYTE	devRequestIndex;
    	UWORD	frequency;
    #ifdef _HOST
    	UBYTE	status;
    	UWORD	RespLength;
    	UBYTE	response[256];
    #endif
    };
    
    
    struct _tagdevRequestPointer devRequestPointer1[] =
    {
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    };
    
    
    
    // Test # 2
    struct _tagdevRequestPointer devRequestPointer2[] =
    {
    	{idx_SET_ADDRESS,1,
    #ifdef _HOST
    	STS_ACK,
    	0x00,
    	{0x00},
    #endif
    	},
    };
    
    // Test # 3
    struct _tagdevRequestPointer devRequestPointer3[] =
    {
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    	// Get Device descriptor
    	{idx_GET_DEVICE_DESCRIPTOR,1,
    #ifdef _HOST
    	STS_Success,
    	0x12,
    	{0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x05,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01},
    #endif
    	},
    }; // End of all test Tables.

    In .c file I declare pointer pointing to arrary of structure. But I need array of such pointers and initlize them( commented code).

    As per me, testTable just one pointer pointing to array of structure. I would like to declare array such pointers and initilize them. Something like above statement. Please let me know.

    Code:
    struct _tagdevRequestPointer (*testTable)[];// = {devRequestPointer1, devRequestPointer2, devRequestPointer3};
    Thanks, RT

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You dont want to declare an array in a header file like that. If more than one file tries to include it, you will get a symbol redefinition error.

    Also, you may want to consider having two different structures instead of using the defines. That would clean up your code quite a bit. As it is now, you code is going to get pretty messy as your application gets bigger.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    You dont want to declare an array in a header file like that. If more than one file tries to include it, you will get a symbol redefinition error.
    Actually this is part of requirement. I want to define these in two different application and I want to let them sahre same definition. SO I have put it in the .h file.

    Also, you may want to consider having two different structures instead of using the defines. That would clean up your code quite a bit. As it is now, you code is going to get pretty messy as your application gets bigger.
    Can you please explain a bit?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Actually this is part of requirement. I want to define these in two different application and I want to let them sahre same definition. SO I have put it in the .h file.
    You don't have to put it in the header file. You'd normally do it like this:
    Code:
    header.h
    
    #ifndef HEADER_H_
    #define HEADER_H_
    
    struct somestruct
    {
      int data;
      char more_data;
    };
    
    #endif
    Code:
    file1.c
    
    struct somestruct struct_instance;
    Code:
    file2.c
    
    extern somestruct struct_instance;
    In this way, struct_instance is shared between the 2 .c files. You can also place the extern somestruct struct_instance; in the .h file to make it shared between all the .c files in your project. Having the extern in the header file doesn't cause the redeclaration problem that having the declaration in the header file causes.

    EDIT: Er, maybe I'm misundertanding. I thought you meant more than one file in the same application for some reason. Still, it's not good practice to put variable declarations in header files. Look through all your libc header files and you'll notice they don't do it there either. There's a reason for it.
    Last edited by itsme86; 09-11-2004 at 07:03 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    The reason, I put in same .h file.

    1. It is not same application, .h file is shared by two different application.

    2. I have defined the structure and created different instances and INITILIZED at the same time. If I do not initlize them in header file, I need to intilize them in two application seperately.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Roaring_Tiger
    Here is what I am trying to do.
    Might a union work, along with some preprocessing trickery?
    Code:
    /* myproj.h */
    
    struct devrqstGeneral
    {
       UBYTE devReqPointer;
       UWORD frequency;
    };
    
    struct devrqstHost
    {
       UBYTE devReqPointer;
       UWORD frequency;
       UBYTE Status;
       UWORD RespLength;
       UBYTE response[256];
    };
    
    union devrqst
    {
       struct devrqstHost     host;
       struct devrqstGeneral  general;
    };
    
    union devrqst devRequest1[]
    #ifdef MAIN
    =
    {
       {
          idx_GET_DEVICE_DESCRIPTOR,
          1,
          /* Host only */
          STS_Success,
          0x12,
          0x12,0x01,0x00,0x02,0x00,0x00,0x00,0x40,0xDC,0x0,/*...*/5,0x00,0x02,0x00,0x00,0x01,0x02,0x03,0x01
       },
       /* ... */
    }
    #endif
    ;
    Code:
    #include <stdio.h>
    
    typedef unsigned char UBYTE;
    typedef unsigned int  UWORD;
    
    #define idx_GET_DEVICE_DESCRIPTOR 1
    #define STS_Success 1
    
    #define MAIN
    #include "myproj.h"
    
    int main(void)
    {
       printf("devRequest1[0].host.RespLength = %u\n", devRequest1[0].host.RespLength);
       return 0;
    }
    
    /* my output
    devRequest1[0].host.RespLength = 18
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Dave,

    Thanks for efforts..

    But as per my pervious post, I want to do this.

    PHP Code:
    In .c file I declare pointer pointing to arrary of structureBut I need array of such pointers and initlize themcommented code). 

    As 
    per metestTable just one pointer pointing to array of structureI would like to declare array such pointers and initilize themSomething like above statementPlease let me know.

    Code:
    struct _tagdevRequestPointer (*testTable)[];// = {devRequestPointer1, devRequestPointer2, devRequestPointer3}; 
    Now I think, I got it done by implementing something like below.

    Code:
    typedef struct _tagdevRequestPointer (*testTable)[]; //= {devRequestPointer1, devRequestPointer2, devRequestPointer3};
    
    testTable thetestTable[] = {devRequestPointer1, devRequestPointer2, devRequestPointer3};
    Now, testTable is pointer to array of structure and theTestTable is array of such pointer. Now I have initilized with array of structure. But I am getting folowing warnings.

    Code:
    warning C4047: 'initializing' : 'struct _tagdevRequestPointer (*)[]' differs in levels of indirection from 'struct _tagdevRequestPointer *'
    warning C4047: 'initializing' : 'struct _tagdevRequestPointer (*)[]' differs in levels of indirection from 'struct _tagdevRequestPointer *'
    warning C4047: 'initializing' : 'struct _tagdevRequestPointer (*)[]' differs in levels of indirection from 'struct _tagdevRequestPointer *'
    But if print, theTestTable[0] , which is equal to address of devRequestPointer1(defined in .H file). So I can ignore warnings.

    But my next challenge is, theTestTable[0], which is pointing to devRequestPointer1. But I am not able to get the number of _tagdevRequestPointer entries from theTestTable[0].

    I know it should be similar to this.

    int a[] = (1,2,3,4);

    number of entries = sizeof(a)/sizeof([0]);

    Any ideas I would I get number of entries in devRequestPointer1 from theTestTable array.

    Looks like it is simple, since I am thinking about this for long time, not getting it.. Need a break... -

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Try something like this
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    // ignore - random bits just so I can compile
    typedef unsigned char UBYTE;
    typedef unsigned short UWORD;
    #define _HOST
    enum {
        idx_GET_DEVICE_DESCRIPTOR,
        idx_SET_ADDRESS,
        STS_Success,
        STS_ACK,
    };
    
    // These two structs go in a header file
    struct _tagdevRequestPointer {
        UBYTE devRequestIndex;
        UWORD frequency;
    #ifdef _HOST
        UBYTE status;
        UWORD RespLength;
        UBYTE response[256];
    #endif
    };
    struct foo {
        struct _tagdevRequestPointer *array;
        size_t len;
    };
    
    // All these array initialisers go in say data.c
    struct _tagdevRequestPointer devRequestPointer1[] = {
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
    };
    
    
    
    // Test # 2
    struct _tagdevRequestPointer devRequestPointer2[] = {
        {idx_SET_ADDRESS, 1,
    #ifdef _HOST
         STS_ACK,
         0x00,
         {0x00},
    #endif
         },
    };
    
    // Test # 3
    struct _tagdevRequestPointer devRequestPointer3[] = {
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
    };                              // End of all test Tables.
    
    struct foo bar[] = {
    #define MKENTRY(x)  { x, sizeof(x)/sizeof(x[0]) }
        MKENTRY(devRequestPointer1),
        MKENTRY(devRequestPointer2),
        MKENTRY(devRequestPointer3),
    };
    size_t barlen = sizeof(bar)/sizeof(bar[0]);
    
    // these are all you need to use the above from anywhere
    extern struct foo bar[];
    extern size_t barlen;
    int main()
    {
        int i;
        for (i = 0; i < barlen; i++) {
            printf("Table %p contains %lu items\n", bar[i].array, bar[i].len);
        }
        return 0;
    }
    Edit:
    The name space abuse is your problem to fix, not mine.
    That's all those with a leading underscore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Let me understand what you are trying to do.

    1. enum {
    idx_GET_DEVICE_DESCRIPTOR,
    idx_SET_ADDRESS,
    STS_Success,
    STS_ACK,
    };
    Instead of declaring as constants, declare in enum. Sounds good.

    2. struct foo {
    struct _tagdevRequestPointer *array;
    size_t len;
    };
    Struct foo, first parameter is pointer to tagdevRequestPointer and second parameter is length. Here is my question. Please read below.


    3. struct foo bar[] = {
    #define MKENTRY(x) { x, sizeof(x)/sizeof(x[0]) }
    MKENTRY(devRequestPointer1),
    MKENTRY(devRequestPointer2),
    MKENTRY(devRequestPointer3),
    };
    For simplicity. it
    struct foo bar[] = {
    {devRequestPointer1, lengthPtr1},
    {devRequestPointer2, lengthPtr2},
    {devRequestPointer3, lengthPtr3}
    };
    Correct???
    bar is array of foo, whose elements are pointer to _tagdevRequestPointer and its length. So if devRequestPointer1 jumps four structure further, we will have devRequestPointer2 as definition of devRequestPointer is contiguous.
    Is my understanding correct?

    Let me know..

    RT

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well having had another look at your code, it's obvious that you haven't got this whole pointer-vs-array deal sorted out, since you've named all your arrays as "pointer" when they're not.

    So, your code again, with incorrect use of the word "pointer" removed
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    // ignore - random bits just so I can compile
    typedef unsigned char UBYTE;
    typedef unsigned short UWORD;
    #define _HOST
    enum {
        idx_GET_DEVICE_DESCRIPTOR,
        idx_SET_ADDRESS,
        STS_Success,
        STS_ACK,
    };
    
    // These two structs go in a header file
    struct tagdevRequest {
        UBYTE devRequestIndex;
        UWORD frequency;
    #ifdef _HOST
        UBYTE status;
        UWORD RespLength;
        UBYTE response[256];
    #endif
    };
    struct foo {
        struct tagdevRequest *array;  // the * tells you its a pointer, that should be enough
        size_t len;
    };
    
    // All these array initialisers go in say data.c
    struct tagdevRequest devRequest1[] = {
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
    };
    
    
    
    // Test # 2
    struct tagdevRequest devRequest2[] = {
        {idx_SET_ADDRESS, 1,
    #ifdef _HOST
         STS_ACK,
         0x00,
         {0x00},
    #endif
         },
    };
    
    // Test # 3
    struct tagdevRequest devRequest3[] = {
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
        // Get Device descriptor
        {idx_GET_DEVICE_DESCRIPTOR, 1,
    #ifdef _HOST
         STS_Success,
         0x12,
         {0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
          0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
    #endif
         },
    };                              // End of all test Tables.
    
    struct foo bar[] = {
    #define MKENTRY(x)  { x, sizeof(x)/sizeof(x[0]) }
        MKENTRY(devRequest1),
        MKENTRY(devRequest2),
        MKENTRY(devRequest3),
    };
    size_t barlen = sizeof(bar)/sizeof(bar[0]);
    
    // these are all you need to use the above from anywhere
    extern struct foo bar[];
    extern size_t barlen;
    int main()
    {
        int i;
        for (i = 0; i < barlen; i++) {
            printf("Table %p contains %lu items\n", bar[i].array, bar[i].len);
        }
        return 0;
    }
    > Struct foo, first parameter is pointer to tagdevRequestPointer and second parameter is length
    If by length, you mean the number of elements in the array, then yes.
    If you mean the number of bytes in the array, then no.
    Look at the use of sizeof() inside the macro MKENTRY

    > For simplicity. it
    Nope, that's not what I wrote

    > bar is array of foo,
    Yes.

    > So if devRequestPointer1 jumps four structure further, we will have devRequestPointer2
    > as definition of devRequestPointer is contiguous.
    It wouldn't matter if they were not contiguous. Each entry of the bar array describes a separate object.
    This would be just as valid
    Code:
    struct foo bar[] = {
    #define MKENTRY(x)  { x, sizeof(x)/sizeof(x[0]) }
        MKENTRY(devRequest3),
        MKENTRY(devRequest1),
        MKENTRY(devRequest2),
    };
    size_t barlen = sizeof(bar)/sizeof(bar[0]);
    > Is my understanding correct?
    Not yet
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile time of C versus C++ code
    By circuitbreaker in forum C++ Programming
    Replies: 20
    Last Post: 02-06-2008, 06:26 PM
  2. Bugging ... Compile Time Error
    By keats in forum C Programming
    Replies: 7
    Last Post: 03-25-2007, 04:54 AM
  3. Replies: 0
    Last Post: 11-29-2002, 10:24 PM
  4. compile time errors!!!!
    By devour89 in forum C++ Programming
    Replies: 6
    Last Post: 11-18-2002, 05:02 PM
  5. Debugging mode selection during compile time
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 09-03-2001, 09:56 AM