Find the answer to your Linux question:
Results 1 to 6 of 6
Hello I am working on embedded linux and got to hit the ground running I started to read some code, and saw a different type of struct than normal C ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    4

    Newbie question


    Hello

    I am working on embedded linux and got to hit the ground running

    I started to read some code, and saw a different type of struct than normal C struct... Could someone explain how this works, especially the "." instead of the typical variable type (int, char, etc)?

    #define DEFINE_DEV(_name, _id) \
    static u64 _name##_id##_dma_mask = DMA_32BIT_MASK; \

    static struct platform_device _name##_id##_device = { \
    .name = #_name, \
    .id = _id, \
    .dev = { \
    .dma_mask = &_name##_id##_dma_mask, \
    .coherent_dma_mask = DMA_32BIT_MASK, \
    }, \
    .resource = _name##_id##_resource, \
    .num_resources = ARRAY_SIZE(_name##_id##_resource), \
    }

  2. #2
    Linux Enthusiast Bemk's Avatar
    Join Date
    Sep 2008
    Location
    Oosterhout-NB, Netherlands
    Posts
    525
    I think this macro is used to create a structure with the parameters of the macro embedded in the name of the structure.

    I think this might be part of the driver code, but I'm not very sure as I only have seen tiny bits of kernel code.

    I would agree though that there are pieces of code which are much more readable.

  3. #3
    Just Joined!
    Join Date
    May 2011
    Location
    Taiwan
    Posts
    5
    For example, if there is a structure like this

    struct ABC{
    int A;
    float B;
    char C;
    };

    then , you can verify if the code below works on your machine

    int main(void){

    struct ABC obj = {
    .A = 10,
    .B = 3.5,
    .C = 'K'
    };
    return 0;
    }

    instead of writing the longer one

    struct ABC obj;
    obj.A = 10;
    obj.B = 3.5;
    obj.C = 'K';

    In my opinion, the syntax(notation) is rare in any programming primers, but it may be useful when you have to deal with lots of structure initializations.
    Back to your question. when you declare that structure, the compiler knows the size of each member, there is no need to specify what data type the member is. so I think the '.' notation helps to save duplicated works(variable name).

    Actually, I didn't know it until now

  4. #4
    Just Joined!
    Join Date
    May 2011
    Posts
    6
    Lets take the same example given by NonerKao.
    struct ABC{
    int A;
    float B;
    char C;
    };

    Suppose, if you want to initialize an array.. the following is the syntax...
    struch ABC obj = {1, 2.3, 'a'};
    In this case, you can't do partial initializations such as initialize only A & C variables.

    The . notation helps us to do partial initializations, which are helpful in many cases.
    So, to initialize A & C variables alone, we can do as follows:
    struct ABC obj = {.A = 1, .C = 'a'};

  5. #5
    Just Joined!
    Join Date
    May 2011
    Posts
    4
    Thanks!

    That makes sense and is very helpful. I understood now.

    How about ## in the define ?

    #define DEFINE_DEV(_name, _id) \
    static u64 _name##_id##_dma_mask = DMA_32BIT_MASK

    Does ## mean "append" ?

    So, here:

    #define DEFINE_DEV(mytestname, mytestid)

    will give me this?

    static u64 mytestname_idmytestid_dma_mask = DMA_32BIT_MASK



    I see many of this kind of complex macros. How do they debug if their macro work ? I am curious.

  6. #6
    Just Joined!
    Join Date
    May 2011
    Posts
    6
    you are correct.
    ## is used to append.

    They can test with some of his test code before release . But it is very common in embedded programming. so, no need to test also... it will work...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •