Thread: Why is the output 9?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    Question Why is the output 9?

    I am still a beginner. Can someone explain to me why the output of this program is 9?
    Code:
    #include<stdio.h>
    #include<string.h>
    void f(int * w) {*w = *w+sizeof(w);}
    main(){
       int a=5;
       f(a);
       printf("%d\n", a);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably should have written:
    Code:
    #include<stdio.h>
    
    void f(int *w)
    {
        *w = *w + sizeof(w);
    }
    
    int main(void)
    {
        int a = 5;
        f(&a);
        printf("%d\n", a);
    }
    Notice that:
    • #include<string.h> was removed as none of the functions declared within it were used.
    • The code for function f has been formatted properly.
    • main has been declared as int main(void), not just as main(). You should declare the return type, and while it is optional in a function definition, you should declare the parameter list as void if the function takes no arguments.
    • f(a) has been changed to f(&a). f(a) is wrong since a is an int, but f expect a pointer to int as the argument.
    • I kept to the leaving out of the return statement from main as it is a special case since C99, but if you are compiling with respect to pre-C99, you should have a return 0; at the end of main.


    Quote Originally Posted by SauceGod
    Can someone explain to me why the output of this program is 9?
    Since &a was passed to f, w points to a, so *w is a, i.e., it has a value of 5. sizeof(w) presumably is 4 for you, so the result is 9. Note that the size of a pointer is not necessarily 4 bytes, so you could end up with a different valid result, e.g., if sizeof(w) == 8, the result would be 13.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It seems "why is the result XYZ?" is becoming your theme, OP. Wouldn't it make sense to experiment and try to find out why these results are happening?
    Code:
    #include <stdio.h>
    void f(int *w)
    {
        *w = *w + sizeof(w);
    }
    int main(void)
    {
        int a = 5;
        printf("value of a=%d\n", a);
        printf("value of sizeof(&a)=%d\n", (int) sizeof(&a));
        f(&a);
        printf("result of f(&a)=%d\n", a);
        return 0;
    }
    Just an idea. Sometimes a little inspection goes a long way.
    Last edited by whiteflags; 06-29-2016 at 07:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2016, 08:27 AM
  2. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  3. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  4. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM

Tags for this Thread