0

so I'm pretty new to C and I'm a bit lost. I have an array of unsigned ints and I have a function that reverses the order of the array using pointers. in the function I pass 2 pointers, one for the start of the array and one for end of the array like so:

void swapedints(unsigned int *x, unsigned int *y) {
    while (x != y) {
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
        x++;
        y--;
    }
}

When I'm calling the function in my main the starter pointer is defined as : &unsignedArray and for the end of the array i used : endArray = (unsigned int *)(&unsignedArray +1) -1

When I pass this to the function I get a segmentation fault, can anybody explain why?

Update w/ correct solution:

    void swapedints(unsigned int *x, unsigned int *y) {
while (x < y) {
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
    x++;
    y--;
 }
}

startPointer = unSignedarray endPointer = unSignedArray + array length -1

ocxt
  • 19
  • 4
  • 7
    It would be easier to show the code than to try to explain it. – Amadeus Oct 08 '19 at 02:11
  • 2
    The `endArray` calculation seems suspect. I agree with the other commenter that you should post the code. See [mcve]. – user3386109 Oct 08 '19 at 02:13
  • 1
    is it c or is it c++? it can't be both – Breakpoint Oct 08 '19 at 02:13
  • 1
    *and for the end of the array i used : endArray = (unsigned int *)(&unsignedArray +1) -1* -- The end of the array is `unsigned int *endArray = (unsignedArray + length-of-array);`. How did you come up with your strange calculation? – PaulMcKenzie Oct 08 '19 at 02:19
  • @PaulMcKenzie shouldn't it be *endArray = (unsignedArray + length-of-array) -1 ? Suppose the starting is at address 100 and length 3. Array is |100|101|102| So end is starting address(100) + length(3) - 1. – Breakpoint Oct 08 '19 at 02:22
  • Your function is going to fail if the arrays don't meet int the middle. For example, if you call with `swapints(arr, arr + 1)` or any other odd increment. – giusti Oct 08 '19 at 02:26
  • Should the while predicate be `x >= y`? That is, to circumvent the problem described by @giusti? – Jeff Holt Oct 08 '19 at 02:31
  • @TotallyNoob -- Depends on the usage. Since this question was originally tagged as C++, the "end of an array" could mean one element past the last valid element (used in various algorithms and containers). – PaulMcKenzie Oct 08 '19 at 14:30

1 Answers1

1

This is likely due to how you write your loop. Your stop condition is x != y. If the array has an even number of elements, this will never be true.

If you are 100% sure that both pointers will point to elements inside your array, you can do like this:

void swapedints(unsigned int *x, unsigned int *y) {
    while (x < y) {
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
        x++;
        y--;
    }
}

It is important that you make sure x and y both point to elements of your array. Otherwise your program might fail in strange ways. For example, the condition x < y might always be true even though they are pointing to completely different things.

I'm also very suspicious to your example for calculating the end of array. This arithmetic you have showed us makes little sense. You should probably use that function in a way like the following:

int main(void)
{
    int array[6] = {1, 2, 3, 4, 5, 6};
    swapedints(array, array + 5);
    // print the array
}
giusti
  • 3,156
  • 3
  • 29
  • 44
  • thank you so much! your solution seemed to work. The calculation I had for endPointer was one I used for a similar problem but it obviously didnt work, thank you again! – ocxt Oct 08 '19 at 02:49