Thread: const declaration help needed

  1. #1
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    const declaration help needed

    I have a function similar to the following:

    Code:
    void print_utility(const void *rec)
    {
            /* simply a test as to whether I can change a var in rec */
            ((struct some_data *)rec)->x = 5;
    }
    whereby rec is a pointer to some structure. I want to ensure the caller of my function that I am not going to change the contents of their rec structure. I've declared the variable as const void *rec but noticed that I was allowed to change the value of a variable in the structure. How do I correctly declare this parameter so that I will not be allowed to change the contents of it?

    Or did I perhaps declare it correctly but since I'm casting a pointer to the rec structure inside the function that this is overiding the const function declaration?-Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Or did I perhaps declare it correctly but since I'm casting a pointer to the rec structure inside the function that this is overiding the const function declaration?
    There's your answer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM