2

I want to make a bubble sort function of an array of pointers that each of the pointers point to another arrays - inside of function and i'm getting a error that i violated a writing location (Visual Studio)

P.S, I do (*parr)++ because the first value of each array shows the length of the array without the first value so i need to start bubble sorting from the second box (arr[1] and not arr[0] for example ). can someone write to me how can i fix it? Thanks for help (I need to sort the values of the original arrays not the pointer of the arrays).

int main(void){

    int i = 0;
    int arr0[4] = { 3, 9, 6, 7 };
    int arr1[3] = { 2, 5, 5 };
    int arr2[1] = { 0 };
    int arr3[2] = { 1, 6 };
    int arr4[5] = { 4, 5, 6, 2, 1 };
    int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
    func1(parr);


    system("PAUSE");
    return (0);
}
void func1(int** parr)
{
    int i;
    int temp;
    int j;
    int k;
    int length;
    for (i = 0; i < 5; i++, (parr)++)
    {
        length = **parr;
        (*parr)++;
        for (j = 0; j < length-1; j++)
        {
            for (k = 0; k < length - j - 1; k++, (*parr)++)
            {
                if ((**parr)>(*(*parr + 1)))
                {
                    temp = **(parr);
                    **(parr) = (*(*parr + 1));
                    (*(*parr + 1)) = temp;
                }
            }
        }


    }


}
11
  • So.. you have five int array, and you're trying to bubble sort each array? Or you have five int array and you're trying to bubble sort them all as-if one consecutive array? Update the question with the answer please. And I don't think (*parr)++ is doing what you think, but I may be misunderstanding your question. And all of this is assuming you actually setup the pointer array correctly; something your question shows no evidence of whatsoever. Commented Apr 18, 2015 at 10:35
  • Seems you must use another pointer variable to sort the target arrays. parr points to the array that has the pointers to the other arrays. Take one element of parr, sort that array (using the other pointer var), now take the next entry of parr, sort that array (using the other pointer var), etc. Commented Apr 18, 2015 at 10:37
  • That's what I mean, I don't know if all of you understood: parr[5] = {arr0,arr1,arr2,arr3,arr4} arr0[5] = ???? and so on. I will try the fixes you guys offered Commented Apr 18, 2015 at 10:40
  • please post a complete example - add the calling code. Commented Apr 18, 2015 at 10:48
  • Sorry for double posting but @WhozCraig do you think it's something in the main function that I haven't done correct? Commented Apr 18, 2015 at 10:49

1 Answer 1

1

This seems to work. It is easier in func1 to use dereferenceing as parr[i][k] rather than moving the pointer.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void func1(int** parr);

int main(void){
    int j;
    int arr0[4] = { 3, 9, 6, 7 };
    int arr1[3] = { 2, 5, 5 };
    int arr2[1] = { 0 };
    int arr3[2] = { 1, 6 };
    int arr4[5] = { 4, 5, 6, 2, 1 };
    int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
    func1(parr);
    for (j = 1; j <= arr0[0]; j++)
    {
        printf ( "arr0[%d] %d\n", j, arr0[j]);
    }
    for (j = 1; j <= arr4[0]; j++)
    {
        printf ( "arr4[%d] %d\n", j, arr4[j]);
    }
    return (0);
}

void func1(int** parr)
{
    int i;
    int temp;
    int j;
    int k;
    int length;
    for (i = 0; i < 5; i++)
    {
        length = **parr;

        for (j = 0; j < length; j++)
        {
            for (k = 1; k < length - j; k++)
            {
                temp = *((*parr)+k);
                if (*((*parr)+k)>*((*parr)+k+1))
                {
                    temp = *((*parr)+k);
                    *((*parr)+k) = *((*parr)+k+1);
                    *((*parr)+k+1) = temp;
                }
            }
        }
        *parr++;// next array
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! Like you said, it seems to work so I'll just compare it to my original code and try to see what have I done wrong, thank you once again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.