Thread: Something Nasty

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    8

    Something Nasty

    I don't think this is a bug, it's bigger than that. There's no question that absolutely nothing is wrong with the code (I think). No idea where to start looking either. I didn't believe it until I looked at this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int test = 0;
        printf("%d\n", &test);
        return 0;
    }
    Running this gives some obscure number, 2293620. Running it with a short instead of an int changed it to 2293622. stdio.h and stdlib.h are just from the standard templates I had.

    Any hints for what this is all about? I guess I can describe some tech stuff, if it would help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are trying to print the address of the int. You must do so with &#37;p (and cast to void*).
    If you wish to print the actual, remove the &.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Hehehehe. I'm embarrased now.

    Can you tell it's been a while? (and I wasn't very experienced in the first place...)
    Last edited by string; 06-01-2008 at 01:28 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what's wrong with that? 2293620 is a perfectly good address to store an int variable in. 2293622 is aligned a little weird, but since a short is only two bytes long there you go. (Note that &test can't possibly be zero -- &test is the address at which test is stored, not the value of the variable itself.)

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Homework's done! Thanks so much, I was so worried (like complete panic).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One of those nasty file read/write issues
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2006, 05:20 PM
  2. Nasty linker problem; multiply defined
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2005, 03:06 PM
  3. Nasty looking thing
    By richard_hooper in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 09:40 AM
  4. Nasty 'Bing' Sound
    By PrivatePanic in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2002, 05:48 AM
  5. Nasty LPDIRECDRAW -> LPDIRECTDRAW7 conversion
    By lightatdawn in forum Windows Programming
    Replies: 0
    Last Post: 03-19-2002, 10:36 PM