Thread: Pointer ??

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up Pointer ??

    Can i assign address to a pointer Manually like this????

    can anyone explain this???


    Code:
     int *ptr=0x4000;
    will this throw compile time error in all compilers???

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't believe it results in any kind of compile-time error.
    However it almost certainly wont do what you want, as any attempt to use that pointer will probably result in a run-time error.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Compilers are required to issue a diagnostic for that code. An integer other than a literal zero can't be assigned to a pointer (which means an integer can't be used to initialize a pointer). After issuing a diagnostic, a compiler is free to continue compiling, but it is not required to.

    You can use a cast to convert an integer to a pointer, but of course the results aren't guaranteed to be anything useful.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    So how do you assign a specific address to a pointer?

    What I mean is If I know that a particular piece of hardware writes to a specific memory address and I want to read it...

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    cast it.
    Code:
    int *ptr = (int*)0x4000;

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    sweet. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM