1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

declearation of a function body and their execution

Discussion in 'C' started by shakti, Mar 11, 2013.

  1. shakti

    shakti New Member

    Joined:
    Mar 11, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Dear friends,
    I am beginner in C programming in Linux. I have one code. I want to learn what will the function give result.
    my code is following:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    int main()
    {
     int a[4] = {1, 2, 3, 4};
     printf("array element before funtions %d %d %d %d\n", a[0], a[1], a[2], a[3]);
     change_val(a[4]);
     printf("array element after funtions %d %d %d %d\n", a[0], a[1], a[2], a[3]);
    return 0;
    }
    void change_val(int *w)
    {
      int n = 4, i = 0;
      while(i<n)
         {
            w[i] = w[i] + 2;
            printf("%d\n", w[i]);
            i++;
         }
      printf("array element in funtion %d %d %d %d\n", w[0], w[1], w[2], w[3]);
    }
    When I compiled this code and execute, it shows following output:

    shakti@shakti-Inspiron-1525:~/Desktop/libicp ./processbyreferece

    array element before funtions 1 2 3 4
    Segmentation fault (core dumped)


    I think only 7th line is executing while there are three more printf options (i.e. 9th, 18th and 21th). My question is how can I rectify this code to execute all printf options. I request to all friends and expert to help me.

    Thanks in advance.

    shakti kumar
     
  2. DRK

    DRK New Member

    Joined:
    Apr 13, 2012
    Messages:
    44
    Likes Received:
    3
    Trophy Points:
    0
    change_val function requires address to int, not int value:
    Code:
    change_val(a);
     
    shabbir likes this.

Share This Page