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

8
  • 7
    It would be easier to show the code than to try to explain it. Commented Oct 8, 2019 at 2:11
  • 2
    The endArray calculation seems suspect. I agree with the other commenter that you should post the code. See minimal reproducible example. Commented Oct 8, 2019 at 2:13
  • 1
    is it c or is it c++? it can't be both Commented Oct 8, 2019 at 2: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? Commented Oct 8, 2019 at 2: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. Commented Oct 8, 2019 at 2:22

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.