Thread: Doubt regarding pointer

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt regarding pointer

    In the code below

    Code:
    int main()
    {
         char *p;
         p = 12;
         *p = 20;
    }
    I want a clarification regarding pointer p that in the second stmt, p is getting an address 12 and in 3rd stmt we are assigning the value 20 at the address 12...right?

    If not please give the justifiction in detail....

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Seems right, I think..

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yep. Addresses are typically given in hex, so the location is actually 0x0000000C.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by karthik537 View Post
    In the code below

    Code:
    int main()
    {
         char *p;
         p = 12;
         *p = 20;
    }
    I want a clarification regarding pointer p that in the second stmt, p is getting an address 12 and in 3rd stmt we are assigning the value 20 at the address 12...right?

    If not please give the justifiction in detail....

    Correct.

    The posted code, however, will give warnings in most compilers, as the compiler thinks you are doing something "strange" if you assign an integer constant to a pointer - a cast will remove the warning.

    It will also, almost certainly, cause something to go wrong in your system - either it will change memory that you shouldn't change, or it will crash, because address 12 is not at all available to you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Addresses are typically given in hex
    can u please explain

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    It will also, almost certainly, cause something to go wrong in your system - either it will change memory that you shouldn't change, or it will crash, because address 12 is not at all available to you.
    It's my computer, I can do what I want with it!!

    can u please explain
    BEN10: you do not have to (and usually should never try to) assign pointer addresses unless it's an address that you know has already been assigned to a pointer.
    Code:
    char *ptr, *p2;
    p2=ptr;  //okay
    ptr=12; //not okay
    If you want to see the address assigned by the compiler (or I guess actually the linker?):
    Code:
    printf("%p",ptr);
    Notice, as matsp sayeth, this is the hexadecimal address of a location in the physical memory of the computer.
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Notice, as matsp sayeth, this is the hexadecimal address of a location in the physical memory of the computer.
    Pedantically, it's the virtual address - if you do not deal with kernel code, you never see the actual physical location for anything [assuming we're talking 32-bit OS's on x86, that is].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Strictly speaking, the %p format specifier is only used to print a pointer to void, so it should be:
    Code:
    printf("%p", (void*)ptr);
    Also, it is not guaranteed that the representation of the address will be in hexadecimal, though it is typically the case.
    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

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