Volatile, nested structs

This is a discussion on Volatile, nested structs within the C Programming forums, part of the General Programming Boards category; I can't figure out why I get "warning: assignment discards qualifiers from pointer target type" when I do this: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Volatile, nested structs

    I can't figure out why I get "warning: assignment discards qualifiers from pointer target type" when I do this:
    Code:
    struct b_struct {
        int *data;
    };
    
    static volatile struct a_struct {
        struct b_struct b;
        int data[10];
    } a;
    
    
    int main(void)
    {
        volatile struct b_struct b;
        b.data = a.data; /* This line gives a warning. */
        return 0;
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    b.data is “volatile pointer to int”, a.data is “array of volatile int” which turns into “pointer to volatile int”.

    That is, with the array of int, each int is volatile. With the pointer, the pointer itself is volatile.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I thought volatile was treated the same way as const, i.e. this makes a "pointer to const int":
    Code:
    const int *ptr;
    So this makes a "volatile pointer to int" and not a "pointer to volatile int"?
    Code:
    volatile int *ptr;
    EDIT: Okay I got it. Putting volatile on the struct makes each variable volatile, which for pointers means that the actual pointer becomes volatile and not the data it points to.
    Last edited by Memloop; 03-28-2010 at 05:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested structs
    By adamantinex in forum C Programming
    Replies: 1
    Last Post: 09-14-2009, 01:13 AM
  2. nested structs
    By bandal27 in forum C Programming
    Replies: 26
    Last Post: 12-13-2008, 08:14 PM
  3. Nested structs
    By Sir Andus in forum C Programming
    Replies: 5
    Last Post: 11-28-2006, 09:31 AM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. Nested Structs
    By Monkey83 in forum C Programming
    Replies: 2
    Last Post: 09-15-2006, 11:03 AM

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