gpt4 book ai didi

c - findString 函数返回错误值

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:08 25 4
gpt4 key购买 nike

<分区>

使用下面的代码:

#include    <stdlib.h>
#include <stdio.h>

/*
* === FUNCTION =========================================================
* Name: findString
* Description: Determines if one character string exists inside other
* string. If found, returns location in source string. If
* not, returns -1.
* ========================================================================
*/

int findString ( const char source[], const char search[] )
{
int i, j;

for ( i = 0; source[i] != '\0'; i++ )
for ( j = 0; source[i + j] == search[j]; j++ )
if ( search[j] == '\0' )
return i;

return -1;
} /* ----- end of function findString ----- */

/*
* === FUNCTION =========================================================
* Name: main
* Description: Displays result of findString function.
* ========================================================================
*/

int main ( void )
{
const char source[] = "hello, world";
const char search[] = "lo, ";
int findString ( const char source[], const char search[] );

printf ( "searching \"%s\" in \"%s\" returns %d.\n",
search, source, findString ( source, search ) );

return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

我希望 findString 函数返回 3,但它返回 -1。运行 gdb 时,源字符串打印“hello, world”,搜索打印“lo,”。但是一旦我进入 findString 函数,源字符串打印“lo,”,搜索字符串打印“hello, world”。为什么他们被调换了?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com