Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   declearation of a function body and their execution (http://www.go4expert.com/forums/declearation-function-body-execution-t29537/)

shakti 11Mar2013 15:28

declearation of a function body and their execution
 
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

DRK 12Mar2013 18:36

Re: declearation of a function body and their execution
 
change_val function requires address to int, not int value:
Code:

change_val(a);


All times are GMT +5.5. The time now is 18:51.