Thread: Unobvious output

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Red face Unobvious output

    Code:
     #include <stdio.h>
    
    void func(int a, int b)
    {
           int * p; 
           p = &b ; 
           p -= 2 ;
           (*p)+= 10; 
    }
    
    main()
    {
           int num;
           num = 0;
           func(1,2);
           num = 12;
           printf(" Num is now %d \n",num);
    }
    the output:

    Code:
    Num is now 0

    why num is 0??
    It must be 12.. I guess

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, that program results in undefined behaviour, methinks. You made p point to b in func(), but then reduced p by 2. This means that p no longer points to b, so your (*p)+= 10; line changes something other than b.

    You probably wanted to write:
    Code:
    #include <stdio.h>
    
    void func(int a, int b)
    {
        int * p;
        p = &b;
        *p -= 2;
        *p += 10;
    }
    
    int main()
    {
        int num;
        num = 0;
        func(1, 2);
        num = 12;
        printf(" Num is now %d \n", num);
        return 0;
    }
    With this revised program, I get the output you expected.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Changing the value that is 2 integer sizes before b in the memory is pretty undefined - that could be ANYTHING. Someone who wrote this code obviously meant for it to modify the "num" value, but there's no guarantee whatsoever that there isn't "stuff" between num and b that is there because the compiler decided to put other stuff there.... Such as the program counter, frame pointer, locally stored registers, etc, etc.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM