0

I wrote this program while watching a tutorial to compare the difference between 'call-by-value' and 'call-by-reference' in C. But i am getting the error:

Run Command: line 1: 1508 Segmentation fault: 11 ./"$2" "${@:3}"

Help?

main() 
{
int a,b;
scanf("%d %d", &a, &b);
printf("Before Call %d %d", a,b);
exch_1(a,b);
printf("After first call %d %d", a,b);
exch_2(a,b);
printf("After second Call %d %d \n", a,b);  

}

exch_1(i,j)
int i, j;
{
    int temp;
    temp = i;
    i = j;
    j = temp;
}

exch_2(i,j)
int *i, *j;
{
    int temp;
    temp = *i;
    *i = *j;
    *j = temp;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    `int temp` for `exch_2` should be `int *temp`. Also the arguments you pass must have `&` to mean "get the address of". I have never seen functions defined like that before though. – Jacob Pollack Aug 10 '13 at 01:15
  • 2
    this very very very old C syntax... can you use modern one? – Bryan Chen Aug 10 '13 at 01:16
  • the functions *ought* to have a type, but I believe the standard says that they will default to int (not void) (just Gogols, MicroSoft seems to think so too). Btw, main() should return an int. – Mawg says reinstate Monica Aug 10 '13 at 01:18
  • @JacobPollack ancient syntax. – JackCColeman Aug 10 '13 at 01:19
  • @UtsavChatterjee how are you compiling & running? From command line or from an IDE? In an IDE (I recommend NEtBeans), you should run/debug and one step through, line by line until you hit the segmentation fault. When you do, your problem should be clear. If not, please let us know which line causes the problem & we will explain why. – Mawg says reinstate Monica Aug 10 '13 at 01:20
  • Urgently throw away the book from which you copied this code: this syntax hasn't been current for nearly a quarter of a century - in "computer years" it means "forever" :-) – Sergey Kalinichenko Aug 10 '13 at 01:21
  • This syntax is called [K&R syntax](http://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi), for anyone who is still confused. It's perfectly valid, but antiquated. – nneonneo Aug 10 '13 at 01:23

2 Answers2

5

As exch_2 expects addresses as parameters, you would have to call it exch_2(&a,&b);.

You are passing the values, and these are taken as addresses. If e. g. a has a value of 5, the computer would try to use the value at address 5on your computer - which probably is not accessible to your program.

FrankPl
  • 13,205
  • 2
  • 14
  • 40
0

Here is the correct code for your problem . Compile your original code with gcc -Wall . It will give you lot of warnings for your above code and preferably you need to fix all of them. If you do not know linux and gcc please learn it. Do not use old tools like turboC compiler etc

void exch_1(int i, int j);   // declare function prototype for exch_1 --> call by value
void exch_2(int* i, int* j);  // declare function prototype for exch_1 --> call by reference

int main()
{
    int a,b;

    scanf("%d %d", &a, &b);
    printf("Before Call %d %d\n", a,b);

    exch_1(a,b);  // call by value. changes done in exch_1 are not reflected here
    printf("After first call %d %d\n", a,b);

    exch_2(&a, &b);   // --> please see the change here for call by reference, you 
                      //should  pass address as the parameters
                      // changes done in exch_2 can be seen here since we pass address
                      // In you original program you are passing value of a and b as the 
                      // address.When you try to access those values in exch_2 the it leads
                      // to undefined behavior and so you can get segfault as well.
    printf("After second Call %d %d \n", a,b);

    return 0;    
}

void exch_1(int i,int j)
//int i, j;    // you do not need these variables
{
    int temp;
    temp = i;
    i = j;
    j = temp;
}

void exch_2(int* i,int* j)
//int *i, *j;   // this line not needed
{
    int temp;
    temp = *i;
    *i = *j;
    *j = temp;
}
stev
  • 182
  • 1
  • 11