Thread: pointer doubt....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    pointer doubt....

    I am a bit puzzled by the following program (found at one of the tutorials on const keyword)

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	const int my_age = 24;
    
    	printf("\n%d", my_age);
    	//my_age = 25; Will generate error
    
    	printf("\n%p", &my_age);
    	printf("\n%p", my_age);
    
    	*(int *)(&my_age) = 25;
    	printf("\n%d", my_age);
    	printf("\n%p", &my_age);
    	printf("\n%p", my_age);
    
    	return 0;
    }
    Though const prefixed variable cannot be changed but it said that using the pointer notation it could be changed. That is fine. My doubt is that i have my_age equal to some integer value say it is 25. And address of my_age is say 0x000012ff.

    Now the notation that these guys use is

    *(int *)(&my_age) = 25;

    I can understand what it is doing. But when i went i thought that even using the following expression should change the value

    *(&my_age) = 25; (Value at address of my_age equals to 25, but it does not work)

    Why appending (int *) works ? I can understand that its a typecast or may be because by default (&my_age) returns a (void *).

    Any thoughts ????????

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    It's because pointers themselves don't have a type, just a location. You must tell the pointer what type the value you're assigning it is(be it an int, a char, a bool, etc...).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, that's not right. Pointers do have a type.
    And the reason it isn't working is because my_age is of type const int, thus if you take its address, we get a pointer to const it (const int*), which if we dereference, we get back to our previous type: const int.
    The cast changes the type of the pointer from const int* to int*, then dereferences it. It circumvents the type system, so to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    And that's why by using the statement

    *(&my_age) = 25;

    the program doesnt compile even (error : lvalue specifies const object) So i am getting back a const int * which is immutable. But by typecasting to (int *) i convert the const int * to int * and hence it becomes mutable.

    I get it now.

    @Elysia and above

    Thanks ! You people are (awesome * googol times) :-)
    Last edited by roaan; 07-27-2009 at 09:24 AM.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Not to mention you shouldn't do this.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @Brafil


    Not to mention you shouldn't do this.
    I didnt get what you meant by that ?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It means you shouldn't "cast away" const, because it's there to protect you from doing mistakes. If you remove the const, then you are no better off than without it.
    In fact, you might be off worse than without it because you are promising to not change it, so anyone who reads the code should assume so much. Then what happens when you do change it? Not a good thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes i get it ;-).

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    That's why they introduced const_cast in c++.

    Code:
    static_cast<long *>(const_cast<int *>(pointer))
    looks cruel (Assuming sizeof(long) == sizeof(int)).

    Not like (long *)pointer. Maybe you didn't know that pointer was actually pointing to const ints?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But this is C, not C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I know. I just showed an example. You should be careful with any cast, and C++ just makes it easier. But in C, I gotta feeling that you don't have to cast too much.

    Just try first without casting. Then, if an error occurs, cast carefully.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Brafil View Post
    But in C, I gotta feeling that you don't have to cast too much.
    No, that's the other way around. You cast a lot in C, much less so in C++.

    Just try first without casting. Then, if an error occurs, cast carefully.
    Now that is an excellent suggestion.
    And also double-check twice before you cast that you are sure you know what you're doing and that you are doing the right thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    No, that's the other way around. You cast a lot in C, much less so in C++.
    Well, I definitely do
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, that's the other way around. You cast a lot in C, much less so in C++.
    Not really. C++ is much more picky when it comes to type checking than C is. There's all kinds of dangerous ways to convert between types in C without even needing a cast.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but good C++ code does not circumvent the type system, so you rarely, if ever, need a cast at all.
    And a lot of conversions in C--without casts--generate warnings from the compiler, so casts to shut it up is a good idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM