如何搜索返回值的位置和比较次数的数组?

时间:2021-10-26 10:16:06

I have a function that searches an array for a given value and returns its position if found. However, I also want it to update the number of comparisons it took to search through the entire array. But it is not working and crashes once it exits the function and calls for the parameter.

我有一个函数,在数组中搜索给定的值,如果找到则返回其位置。但是,我还希望它能够更新搜索整个阵列所需的比较次数。但是一旦退出函数并且调用参数,它就无法工作并崩溃。

int linSearch(int arr[], int size, int target, int* numComparisons) {
    int i;
    int count = 0;

    for (i = 0; i < size; i++) {
        count++;
        if (arr[i] == target) {
            numComparisons = count;
            return i;
        }
    }
    return -999;
}

I am calling the function with this

我用这个调用函数

linSearch(userArray, userEntries, valSearch, *numLinSearch)

and the variable numLinSearch is this

而变量numLinSearch就是这个

int *numLinSearch = 0;

When i try to print out something like printf("%d", numLinSearch); the program crashes. How do I fix this?

当我尝试打印出类似printf(“%d”,numLinSearch)的内容时;程序崩溃了。我该如何解决?

3 个解决方案

#1


2  

numComparisons = count;

You are changing the value of the pointer, not value of the pointed object. It should be rather:

您正在更改指针的值,而不是指向对象的值。应该是:

*numComparisons = count;

numComparisons has to contain the address of a variable so that the function can process its data. Therefore you should call linSearch like this:

numComparisons必须包含变量的地址,以便该函数可以处理其数据。因此你应该像这样调用linSearch:

int numComparisons = 0; /* for example */
linSearch(userArray, userEntries, valSearch, &numLinSearch)

rather than:

而不是:

int *numComparisons = 0; /* numComparisons is a null pointer */

#2


0  

It crashes not when you call printf, but when you call linSearch. The correct call should look like this:

它不是在你调用printf时崩溃,而是在你调用linSearch时崩溃。正确的调用应如下所示:

linSearch(userArray, userEntries, valSearch, &numLinSearch);

to pass numLinSearch by reference

通过引用传递numLinSearch

#3


0  

When you initialize

初始化时

   int * numLinSearch = 0

you set numLinSearch to NULL and when you attempt to dereference it in the method it crashes

您将numLinSearch设置为NULL,当您尝试在方法中取消引用它时,它会崩溃

initialize it as

将其初始化为

    int * numLinSearch = malloc(sizeof(int));

or alternatively, (not recommended if you want to save this pointer out of scope)

或者,(如果您想将此指针保存在范围之外,则不推荐)

    int temp = 0;
    int * numLinSearch = &temp;

#1


2  

numComparisons = count;

You are changing the value of the pointer, not value of the pointed object. It should be rather:

您正在更改指针的值,而不是指向对象的值。应该是:

*numComparisons = count;

numComparisons has to contain the address of a variable so that the function can process its data. Therefore you should call linSearch like this:

numComparisons必须包含变量的地址,以便该函数可以处理其数据。因此你应该像这样调用linSearch:

int numComparisons = 0; /* for example */
linSearch(userArray, userEntries, valSearch, &numLinSearch)

rather than:

而不是:

int *numComparisons = 0; /* numComparisons is a null pointer */

#2


0  

It crashes not when you call printf, but when you call linSearch. The correct call should look like this:

它不是在你调用printf时崩溃,而是在你调用linSearch时崩溃。正确的调用应如下所示:

linSearch(userArray, userEntries, valSearch, &numLinSearch);

to pass numLinSearch by reference

通过引用传递numLinSearch

#3


0  

When you initialize

初始化时

   int * numLinSearch = 0

you set numLinSearch to NULL and when you attempt to dereference it in the method it crashes

您将numLinSearch设置为NULL,当您尝试在方法中取消引用它时,它会崩溃

initialize it as

将其初始化为

    int * numLinSearch = malloc(sizeof(int));

or alternatively, (not recommended if you want to save this pointer out of scope)

或者,(如果您想将此指针保存在范围之外,则不推荐)

    int temp = 0;
    int * numLinSearch = &temp;