Search code examples
carraysfunctionpointersundefined-behavior

Unexpected behaviour of a c program?


This code is not giving the output as expected and clearly i cant understand the program behaviour.Plz help me in understanding this.Any Help will be appreciated. You can see the output of the program https://code.hackerearth.com/31d954z?key=fce7bc8e07fc10a3d7a693e45a5af94a here.

1.In the last comment, I can't find why the elements of array are not updated.

2.In the body of func on printing 'a' it gives some unexpected output.

For e.g., if i pass j = 2 and a[0] = 1

       After j = j+1 , which results in j = 3;

       a=a+j should result in a = 4

       but instead it result in a = 13.
#include <stdio.h>

void func(int j,int *a)
{
 j=j+1;
 printf("%d\t%d\n",a,j);     //prints j updated value and a
 a=a+j;
 printf("a = %d\n ",a);      //prints value of a
}


void main()
{
 int a[5] ={1,2,3,4,5},i,j=2;
 for (i =0;i<5;i++ )
 func(j, a[i]);
 for (i =0;i<5;i++ )   
 printf("%d\t",a[i]);    //Prints the array as 1 2 3 4 5
}

On running this code,Output is :

1 3 // Here a = 1 and j = 3 a = 13 //but after addition a = 13 2 3 a = 14 3 3 a = 15 4 3 a = 16 5 3 a = 17 1 2 3 4 5 //array elements not updated


Solution

  • I want to understand the code behavior.

    Your code produces Undefined Behavior, thus you should stop what you are doing and debug it.


    When you want to index arrays you do it like this:

    a[i]
    

    where i is the index and a your array. So if you want to access the first element, you need to do a[0], when you want to index the 3rd element, you do a[2] and so on.


    However, what you may want to do is something like passing the i-th element, add it and print it, only.

    So, you should enable your compiler warnings:

    prog.c: In function 'func':
    prog.c:6:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
      printf("%d\t%d\n",a,j);     //prints j updated value and a
              ~^
              %ls
    prog.c:8:15: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
      printf("a = %d\n ",a);      //prints value of a
                  ~^
                  %ls
    prog.c: At top level:
    prog.c:12:6: warning: return type of 'main' is not 'int' [-Wmain]
     void main()
          ^~~~
    prog.c: In function 'main':
    prog.c:16:10: warning: passing argument 2 of 'func' makes pointer from integer without a cast [-Wint-conversion]
      func(j, a[i]);
              ^
    prog.c:3:6: note: expected 'int *' but argument is of type 'int'
     void func(int j,int *a)
          ^~~~
    

    and then modify your code accordingly, for example like this:

    #include <stdio.h>
    
    void func(int j,int a)
    {
     j=j+1;
     printf("%d\t%d\n",a,j);   
     a=a+j;
     printf("a = %d\n ",a);
    }
    
    
    int main(void)
    {
     int a[5] ={1,2,3,4,5},i,j=2;
     for (i =0;i<5;i++ )
     func(j, a[i]);
     for (i =0;i<5;i++ )   
     printf("%d\t",a[i]);
    }
    

    which outputs:

    1   3
    a = 4
     2  3
    a = 5
     3  3
    a = 6
     4  3
    a = 7
     5  3
    a = 8
     1  2   3   4   5