- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
由于一个指针:p_return_index
,我卡在了这个程序上。正如它所暗示的那样,它被纳入每个搜索功能,并且应该被赋予找到搜索号码的索引值。如果搜索的数字不在数组中,程序运行正常,但是当我输入数组中的数字时,出现段错误(核心转储)错误。传递指针一定是我做错了什么。因为它被赋予了索引号。我只是不知道如何解决这个问题。请注意,指针是用 NULL
初始化的;我认为这可能与它有关。显然,我不知道。
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;
}
帮助将不胜感激。
最佳答案
你正在做的是:
您声明一个指针并为其分配 NULL。
您将此 NULL 值传递给函数。
该函数尝试读取指针后面的内存。由于指针仍存储地址 NULL,因此该函数尝试读取地址 NULL 处的内存,该地址不存在。
使用指针参数作为返回值的习惯用法是这样的:
您可以像以前那样定义一个指针参数。
void foo(int* myReturnArgument);
在调用代码中,您定义了一个不是指针的变量。然后您获取该变量的地址并将其传递给函数。
int bar;
foo(&bar);
在函数中,您操作指针指向的值。
void foo(int* myReturnArgument) {
*myReturnArgument = 42;
}
关于c - 通过函数传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166612/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!