C - 指向数组中索引号的指针

时间:2021-03-19 21:42:43

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.

我在函数中得到一个运行时错误,显示找到该值的索引。(search_results)(当搜索到的数字不在数组中时程序运行正常。)它必须是一个简单的错误,但我认为一双新鲜的眼睛可以帮助你。

everything is an int, except found_status which is a char

一切都是int,除了find_status是一个char

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

这是main()代码。 (必要的东西)

    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.

如果有人能够找出问题所在,对我来说将是一个很大的帮助。如果您需要更多信息,请发表评论,我会尽快回复。

2 个解决方案

#1


1  

p_return_index is initialized with NULL.

p_return_index初始化为NULL。

use int p_return_index[1];

使用int p_return_index [1];

Than in search_results(...)

比在search_results(...)

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

#2


1  

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.

您的代码设置一个指向局部变量地址的本地指针;此更改不适用于调用代码。如果要影响调用代码,则需要更新p_return_index指向的内存。

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.

在main中给你内存以从ordered_seq_search写入。

#1


1  

p_return_index is initialized with NULL.

p_return_index初始化为NULL。

use int p_return_index[1];

使用int p_return_index [1];

Than in search_results(...)

比在search_results(...)

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

#2


1  

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.

您的代码设置一个指向局部变量地址的本地指针;此更改不适用于调用代码。如果要影响调用代码,则需要更新p_return_index指向的内存。

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.

在main中给你内存以从ordered_seq_search写入。