#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile Exchanging values between 2 variables


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    int a[2],b[2],temp,i;
    
    printf("Enter the values of a and b \n");
    scanf("%d %d",&a[i],&b[i]);
    temp=a[i];
    a[i]=b[i];
    b[i]=temp;
    printf("After exchange values of a and b are %d and %d ",a[i],b[i]);    
      
    }
    code runs but ide stops working... any ideas??
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    What's the value of i? It's garbage, which has a very high probability of not being either 1 or zero (the only valid values for an index into those two arrays). By accessing some memory location outside what was allocated for the arrays, you are overwriting something -- that's called clobbering and is a very insidious kind of bug.

    Before you use a variable, remember to always initialize it. That is especially important with pointers, since using a garbage pointer will almost invariably result in you trying to access memory that's not yours resulting in the program crashing "for no reason" due to a segfault.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    what should be correction in my code please tell
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    its done i used i=2; thanks :)

    Comments on this post

    • b49P23TIvg disagrees : good grief
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by swapy
    its done i used i=2; thanks :)
    Which is dead wrong.

    When you declare an array in C, you declare it with the total number of elements in that array. You wrote:
    Code:
         int a[2],b[2],temp,i;
    Thus both arrays contain two elements each.

    The indexing of arrays in C is zero-based, which means that the first element is indexed with 0. That means that the second element is indexed with 1. The third element would be indexed with 2, but in your arrays there is no third element.

    By setting i to 2, you are indexing outside of your arrays, thus clobbering the memory locations next to those two arrays. That is a very bad idea.

    Always remember this basic rule: in an array of n elements, the indexes range from zero to n-1. Always!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    rightly said... fixed it :)

IMN logo majestic logo threadwatch logo seochat tools logo