Jesse Upchurch
Jesse Upchurch

Reputation: 91

C - Pointer to index number in an array

I know I have to be overlooking something simple. I have a program that lets the user observe how different search algorithms probe arrays by displaying the indexes of each probe. I have a pointer that is passed to the sorting algorithm function, and it should be assigned the index number that the searched number was found at. The pointer is then used in another function to display which index the value was found in (if found).

I get an run-time error in the function that displays the index the value was found in. (search_results) (The program runs fine when the searched number is not in the array.) It has to be a simple mistake, but I think a fresh set of eyes could help.

everything is an int, except found_status which is a char

Here is the main() code. (The necessary stuff)

    int *p_return_index = NULL;

/* 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 (0 to quit): "),
        scanf("%d", &searched_number), searched_number != 0)
{
    printf("\n\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);
    }

This is where the pointer is passed to an assigned the index.

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

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

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

And this is where the error is happening.

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);
            //I get the error at the line above.
if(found == 'U')
    printf("Unsuccessful - target not found");
if(found != 'S' && found != 'U')
    printf("Undetermined");
return;
}

If someone can find out what is wrong, it would be a big help to me. If you need more information just comment, and I'll reply as soon as possible.

Upvotes: 0

Views: 2787

Answers (2)

simonc
simonc

Reputation: 42165

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

should be

if(searched_number == array[index] != 0) {
    *p_return_index = index;

Your code sets a local pointer to point to the address of a local variable; this change is not available to calling code. You need to update the memory that p_return_index points to if you are to affect calling code.

This takes us onto the next problem.

int *p_return_index = NULL;

should be

int return_index;

giving you memory in main to write to from ordered_seq_search.

Upvotes: 1

zeyorama
zeyorama

Reputation: 445

p_return_index is initialized with NULL.

use int p_return_index[1];

Than in search_results(...)

if(searched_number == array[index] != 0) {
  *p_return_index = index;

Upvotes: 1

Related Questions