Jesse Upchurch
Jesse Upchurch

Reputation: 91

Passing pointers throughout Functions

I'm stuck on this program because of one pointer: p_return_index. It, as it implies, is taken into each searching function and should be given the index value where a searched for number is found at. The program runs fine if the number searched for is not in the array, but when I enter a number that is in the array, there is a segmentation fault(core dumped) error. I have to be doing something wrong with passing the pointer. Because it is given the index number. I just do not know how to fix that. Notice that the pointer is initialized with NULL; I think that may have something to do with it. I, obviously, do not know though.

void show_data(int array[], int max_index, int searched_number);
/* Display the arrays contents on screen */
void search_results(char found, int *p_return_index);
/* Prints if data is found */
int ordered_seq_search(int array[], int max_index, int searched_number, int *p_return_index);
/* Ordered Sequential Search Algorithm */
int probability_search(int array[], int max_index, int searched_number, int *p_return_index);
/* Probability Search Algorithm */
int binary_search( int array[], int max_index, int searched_number, int *p_return_index);
/* Binary Search Algorithm */

/**********************************************************************/
/*                           Main Function                            */
/**********************************************************************/
int main()
{
    int searched_number,
    *p_return_index = NULL,
    max_index = MAX_INDEX,
    seq_data[DATA_SIZE],
    prob_data[DATA_SIZE],
    bin_data[DATA_SIZE];

    char found_status;

    /* Print the program heading and instructions                      */
    printf("\n\n\n\n\n\n\n");
    print_heading();
    print_instructions();

    /* Fill arrays with data                                           */
    fill_array(seq_data, max_index);
    fill_array(prob_data, max_index);
    fill_array(bin_data, max_index);

    while(printf("\n\n\nEnter an integer search target (%d to quit): ",
            QUIT), scanf("%d", &searched_number), searched_number != 0)
    {
        printf("\n");
        printf("\nOrdered Sequential Search:");
        show_data(seq_data, max_index, searched_number);

        if(ordered_seq_search(seq_data, max_index, searched_number, p_return_index) == 1)
        {
            found_status = 'S';
            search_results(found_status, p_return_index);
        }
        else
        {
            found_status = 'U';
            search_results(found_status, p_return_index);
        }

        printf("\n\n");
        printf("\nProbability Search:");
        show_data(prob_data, max_index, searched_number);
        if(probability_search(prob_data, max_index, searched_number, p_return_index) == 1)
        {
            found_status = 'S';
            search_results(found_status, p_return_index);
        }
        else
        {
            found_status = 'U';
            search_results(found_status, p_return_index);
        }

        printf("\n\n");
        printf("\nBinary Search:");
        show_data(bin_data, max_index, searched_number);
        if(binary_search(bin_data, max_index, searched_number, p_return_index) == 1)
        {
            found_status = 'S';
            search_results(found_status, p_return_index);
        }
        else
        {
            found_status = 'U';
            search_results(found_status, p_return_index);
        }
    }

    printf("\nThanks for searching. Have a nice day! ;-)");
    printf("\n\n\n\n\n");

    return 0;
}

/**********************************************************************/
/*                     Print the program heading                      */
/**********************************************************************/
void print_heading()
{
   printf("\n========================================================");
   printf("\n                Program Number: %d",    PROGRAM_NUMBER);
   printf("\n                Programmer: %s",        PROGRAMMER_NAME);
   printf("\n                PCC Course Number: %s", COURSE_NUMBER);
   printf("\n========================================================");
   return;
}

/**********************************************************************/
/*                   Print the program instructions                   */
/**********************************************************************/
void print_instructions()
{
    printf("\nThis program demonstrates various search algorithms.");
    printf("\nYou enter in any whole number, and the program will");
    printf("\nsearch for it in an ORDERED array of whole numbers");
    printf("\nusing each of the following search algorithms:");
    printf("\n     1. Ordered Sequential Search");
    printf("\n     2. Probability Search");
    printf("\n     3. Binary Search");
    printf("\nThe progress of each search is shown so the efficiency");
    printf("\nof the search algorithms can be compared.");
    return;
}

/**********************************************************************/
/* Fill the array */
/**********************************************************************/
void fill_array(int array[], int max_index)
{
   int index_value;

    for(index_value = 0; index_value <= max_index; index_value++)
        array[index_value] = (index_value * 5) + 10;
    return;
}

/**********************************************************************/
/* Show the data */
/**********************************************************************/
void show_data(int array[], int max_index, int searched_number)
{
   int count;

    printf("\n   Array Index: ");
    for (count = 0; count <= MAX_INDEX ; count++)
        printf("[%2d]", count);
    printf("\n    Array Data: ");
    for (count = 0; count <= MAX_INDEX; count++)
        printf(" %2d ", array[count]);
    printf("\n   User Target:  %2d", searched_number);
    return;
}


/*****************************************************************/
/* Found Stuff */
/************/
void search_results(char found, int *p_return_index)
{
    printf("\nSearch Outcome: ");
    if(found == 'S')
        printf("Successful - target found at index [%2d]", *p_return_index);
            //The run-time error happens here. So, it definitely has to be the pointer.
    if(found == 'U')
        printf("Unsuccessful - target not found");
    if(found != 'S' && found != 'U')
        printf("Undetermined");
    return;
}

/**********************************************************************/
/* Ordered Sequential Search */
/**********************************************************************/
int ordered_seq_search(int array[], int max_index, int searched_number, int *p_return_index)
{
    int index = 0;

    printf("\n   Search Path: ");
    while (printf("[%2d]", index),
             index < max_index && searched_number != array[index - 1] &&
             searched_number > array[index])
    {

        index++;
    }

    if(searched_number == array[index])
    {
        p_return_index = &index;

        printf("\n%d", *p_return_index);
            //This is just to show that `p_return_index` does get the right value.
        return 1;
    }
    else
        return 0;
}

/**********************************************************************/
/* Probability Search */
/**********************************************************************/
int probability_search(int array[], int max_index, int searched_number, int *p_return_index)
{
    int index = 0;
    int temp;

    printf("\n   Search Path: ");
    while (printf("[%2d]", index),
             index < max_index && searched_number != array[index])
    {

        index++;
    }


    if(searched_number == array[index])
    {
        p_return_index = &index;
        if(index > 1)
        {
            temp = array[index - 1];
            array[index - 1] = array[index];
            array[index] = temp;
            array = array - 1;
        }
        return 1;
    }
    else{
        return 0;
    }
}

/**********************************************************************/
/* Binary Search */
/**********************************************************************/
int binary_search( int array[], int max_index, int searched_number, int *p_return_index)
{
    int begin, end, middle;

    begin = 0;
    end = max_index;

    printf("\n   Search Path: ");

    while( begin <= end )
    {
        middle = (begin + end) / 2;
        if(searched_number > array[middle])
        {
            printf("[%2d]", middle);
            begin = middle + 1;

        }
        else if(searched_number < array[middle])
        {
            printf("[%2d]", middle);
            end = middle - 1;
        }
        else
        {
            printf("[%2d]", middle);
            begin = end + 1;
        }
    }

    if(searched_number == array[middle])
    {
        p_return_index = &middle;
        return 1;
    }
    else
        return 0;
}

Help would be really appreciated.

Upvotes: 0

Views: 119

Answers (1)

What you are doing is:

  1. You declare a pointer and assign NULL to it.

  2. You pass this NULL value to a function.

  3. That function tries to read the memory behind the pointer. Since the pointer still stores the address NULL, the function tries to read memory at address NULL, which does not exist.


The idiom for using a pointer parameter as a return value is this:

  1. You define a pointer argument just the way you did.

    void foo(int* myReturnArgument);
    
  2. In the calling code, you define a variable that is not a pointer. Then you take the address of that variable and pass it to the function.

    int bar;
    foo(&bar);
    
  3. In the function, you manipulate the value that the pointer points at.

    void foo(int* myReturnArgument) {
        *myReturnArgument = 42;
    }
    

Upvotes: 2

Related Questions