Thread: question about basic programming

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    11

    question about basic programming

    when running the following code, why do I get two different memory addresses?
    Code:
    #include <stdio.h>
    
    
    int main(void){
    
    
        int  short signed number=-3;
        int short signed *pointer=&number;
        printf("the value of number is %d",number);
        printf("\n it's address in memory is %p",pointer);
        printf("\n it's address in memory is %d",pointer);
    
    
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The pedantically correct way to do this is with:
    Code:
    printf("\n it's address in memory is %p", (void*)pointer);
    Though you probably can get away without the cast to void*.

    Anyway, glossing over the undefined behaviour, you probably got the same address printed, just that they look different, kind of how "one hundred" and 100 (in base ten) are the same number but look rather different.
    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. question about basic programming
    By Maor Elbaz in forum C Programming
    Replies: 3
    Last Post: 12-10-2016, 04:43 PM
  2. Basic C Programming Question
    By goldeneye007 in forum C Programming
    Replies: 1
    Last Post: 12-06-2012, 06:04 PM
  3. basic bash programming question
    By Jimb0 in forum Linux Programming
    Replies: 4
    Last Post: 05-05-2011, 10:55 PM
  4. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  5. Just a basic question about game programming
    By need4speed in forum Game Programming
    Replies: 5
    Last Post: 01-20-2008, 03:24 AM

Tags for this Thread