gpt4 book ai didi

c++ - 从函数返回 char*

转载 作者:行者123 更新时间:2023-11-28 01:33:58 29 4
gpt4 key购买 nike

<分区>

我试图从一个函数返回一个 char* ,该函数通过获取给它的两个字符串的一部分来形成一个字符串。该字符串必须作为 char* 返回。但是当我运行下面的代码时,没有打印输出。请帮助解决这个问题。

#include <iostream>
#include<string.h>
using namespace std;

char *findpass(char *s1, char*s2)
{
int sl1 = strlen(s1);
int sl2 = strlen(s2);
int i = 0, in = 0;
char temp[100];
char *t;
if (sl1 % 3 == 0)
{
for (i = 0; i<sl1 / 3; i++)
{
temp[in] = *(s1 + i);
in++;
}
}
t = temp;
return t;
}

int main()
{
char i1[10], i2[10];
char *f1, *f2;
cin >> i1 >> i2;
f1 = i1;
f2 = i2;
char *f = findpass(f1, f2);
cout << f;
return 0;
}

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