How to print the value at memory address?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shubhangi24
    New Member
    • Oct 2012
    • 20

    How to print the value at memory address?

    I have written a C program without variable. And I want to print the value at that memory location.How to print that value?

    code is like:-

    Code:
    int main()
     {
         printf("Enter value:");
         scanf("%d",1245024);
         /* how to print the value here */ 
      
     return 0;
     }
    Last edited by Meetee; Nov 20 '12, 12:33 PM. Reason: please add code tags <code/> around your code
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You want your program to read some arbitrary location that is outside the bounds of your program.

    Doing so provokes implementation-defined behavior. Anything could happen, but the two most likely outcomes are a run-time error (typically a segment error) from trying to access an inaccessible memory location; or the memory location is successfully accessed.

    Cast the address value into a pointer to the type corresponding to how you wish to access the location (that is, unsigned char* or unsigned short* or unsigned long* or ...). Then dereference the pointer and see what happens.
    Last edited by donbock; Nov 21 '12, 05:15 AM. Reason: Changed "undefined behavior" to "implementation-defined behavior".

    Comment

    • Shubhangi24
      New Member
      • Oct 2012
      • 20

      #3
      I want the answer without any variable not even pointer...

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Put the unsigned integer into the first element of an array and call printf on the array name. Also I don't think I have ever read (or heard)that a pointer could be classified as any form of variable.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Shubhangi24: donbock did not suggest using a variable, he suggested that you cast you integer constant to have a pointer type and then dereferenced it to get the value at that address something like *((unsigned char*)1) no variables involved and a well known technique for accessing registers on an embedded platform..

          whodgson: your array thing wont owrk and uses a variable. Also pointers can't be variables??? Think about it, what do you think have if you declare something like char *p; as used by countless C library functions. p is a variable and it is a pointer.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Here's what the inventor says about it:
            "The places in which we store data are called objects. To access an object we need a name. A named object is called a variable and has a specific type... the data items we put into variables are called values. The statement that defines a variable is (unsurprisingly ) called a definition"
            Last edited by whodgson; Dec 15 '12, 03:47 AM. Reason: add a space

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              to Banfa: I agree having tried it that an array won't even work for me presumably because a variable needs a name before memory is allocated and it becomes an object.
              But I can't see how a pointer is also a variable because i understood that a valid pointer only ever contains the address of whatever it currently points at (or to). Although this has no bearing on the argument i also thought that a pointer had to be initialized at the time it was declared.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Code:
                int a = 10;
                int *b = &a;
                a and b are variables.
                The value of a is 10.
                The value of b is the address of a. That is, b is a pointer to a.

                Comment

                • whodgson
                  Contributor
                  • Jan 2007
                  • 542

                  #9
                  Yes...thanks, I've been back over it and what you both have said is what i have been reading. Some things just seem to go through to the keeper without registering.

                  Comment

                  Working...