Thread: How can any data be assigned to a const member of a structure?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    How can any data be assigned to a const member of a structure?

    I review my study book yesterday. While I was examing a program in the book as you can see below, I noticed something strange. We normally know that we cannot assign data to a variable defined as const type. Namely;

    Code:
    int *const y;
    It represents that Constant Pointer - Non Constant Data.

    Code:
    const int *const z;
    It represents that Consant Pointer - Constant Data.

    Code:
    const int *x;
    It represents that Non Constant Pointer - Constant Data.

    Code:
    int *t;
    It represents that Non Constant Pointer - Non Constant Data.



    -Constant pointer means that we cannot modify pointer variable.(value inside pointer variable)

    -Constant Data means that we cannot modify data inside a variable which is pointed by a pointer variable.



    So how can we assign a data to a const member of a structure in the following code,despite of being constant data?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> 
    
    struct card{
        const char *face; // CONST MEMBER!!!!!!!!!!!!!!!CONST DATA!!!!!!!!!!!!!!!
        const char *suit; // CONST MEMBER!!!!!!!!!!!!!!!CONST DATA!!!!!!!!!!!!!!!
    
    };
    typedef struct card Card;
    void fillDeck(Card *const wDeck,const char *wFace[],const char *wSuit[]);
    void shuffle(Card *const wDeck);
    void deal(const Card *const wDeck);
    int main()
    {
        Card deck[52];
        const char *face[]={"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
        const char *suit[]={"Hearts","Diamonds","Clubs","Spades"};
        srand(time(NULL));
        fillDeck(deck,face,suit);
        shuffle(deck);
        deal(deck);
        return 0;
    }
    void fillDeck(Card *const wDeck,const char *wFace[],const char *wSuit[])
    {
        int i;
        for(i=0;i<=51;i++){
            wDeck[i].face=wFace[i%13];    // ASSIGN DATA TO A CONST MEMBER OF A STRUCTURE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
            wDeck[i].suit=wSuit[i/13];       // ASSIGN DATA TO A CONST MEMBER OF A STRUCTURE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    
        }
    }
    void shuffle(Card *const wDeck)
    {
        int i;
        int j;
        Card temp;
        for(i=0;i<=51;i++){
            j = rand()%52;
            temp = wDeck[j];
            wDeck[j] = wDeck[i];
            wDeck[i] = temp;
        }
    }
    void deal(const Card *const wDeck)
    {
        int i;
        for(i=0;i<=51;i++){
            printf("%5s of %-8s%s",wDeck[i].face,wDeck[i].suit,(i+1)%4 ? " " : "\n");
        }
    }
    OUTPUT:
    How can any data be assigned to a const member of a structure?-outputofstructure-jpg
    Last edited by hefese; 08-14-2012 at 10:02 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hefese
    So how can we assign a data to a const member of a structure in the following code,despite of being constant data?
    The members of the structure are non-const pointers to const char, hence you can assign to them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Quote Originally Posted by laserlight View Post
    The members of the structure are non-const pointers to const char, hence you can assign to them.
    I thought for a moment that pointer face and pointer suit in the structure takes value for a variable dereferenced,when they initializing.Namely;

    Code:
    const char *face; // const char *face=0; (0 means NULL)
    const char *suit; // const char *suit=0; (0 means NULL)
    However, they take address of a variable which face or suit points to, and there is no variable due to NULL at first. Because I've lived in an confusion at this point, I thought that there is a strange situtation in the code. Thank you again for your interest.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If a structure member is truly constant, you might not be able to assign it with "=", but you can at initialization-time.
    Code:
    typedef struct {
        const int test1;
        const int test2;
    } Test;
    
    // This doesn't work.
    Test t;
    t.test1 = 5;  // error: assigning to const value
    
    // Use structure initialization (init members in the order they appear):
    Test t = {42, 127};
    
    // Named-element structure initialization. NOTE: this is C99, part of the newer C standard. For gcc, use --std=c99 to get this to work.
    Test t = {
        .test1 = 42,
        .test2 = 66
    };
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Quote Originally Posted by dwks View Post
    If a structure member is truly constant, you might not be able to assign it with "=", but you can at initialization-time.
    Code:
    typedef struct {
        const int test1;
        const int test2;
    } Test;
    
    // This doesn't work.
    Test t;
    t.test1 = 5;  // error: assigning to const value
    
    // Use structure initialization (init members in the order they appear):
    Test t = {42, 127};
    
    // Named-element structure initialization. NOTE: this is C99, part of the newer C standard. For gcc, use --std=c99 to get this to work.
    Test t = {
        .test1 = 42,
        .test2 = 66
    };

    I understand that the point which I have lived a problem as I mentioned earlier. But, seeing this knowledge you shared, it is settled thoroughly in my mind. Thank you for sharing knowledge in the above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Member never assigned in sample code!!!
    By see the big C in forum C Programming
    Replies: 4
    Last Post: 02-16-2011, 10:19 PM
  2. How to declare const data member in class
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 04:54 AM
  3. Replies: 20
    Last Post: 11-12-2005, 03:10 PM
  4. Mutable members in const member functions still const
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 08:56 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM

Tags for this Thread