Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Help in Output

This is a discussion on Help in Output within the C and C++ forums, part of the Programming Talk category; My friend questioned me the output of the below program #include <stdio.h> main() { int a[] = { 1, 4, ...

  1. #1
    ashlee is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    108

    Help in Output

    My friend questioned me the output of the below program

    #include <stdio.h>
    main()
    {
    int a[] = { 1, 4, 8, 5, 1, 4 };
    int *s,b;
    s = a + 4;
    b = s - a;
    printf("%d",b);
    }

    I told the output as 8.But he told me its wrong and asked me to try. Kindly someone help me to solve this problem.


  2. #2
    Adrian is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    124
    Yes as your friend said your answer was wrong. The output is 4.Its simple. Consider the base address or starting address of a as 1000. So you would have 1000+4=1004 stored in s and again
    b=s-a
    gives
    b=1004-1000=4 and b is outputted which gives the output as 4.


Latest Article

Network Security Risk Assessment and Measurement

Read More...