C++ 程序不返回值(函数)

C++ program not returning a Value (Functions)

我想编写一个程序,创建一个将两个字符串连接成一个字符串的函数。但是,我的程序没有返回任何值。

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

char* my_str_cat( char* s1, char* s2, char* combined ){
   for(int i = 0;i<strlen(s1);i++){
       combined[i]=s1[i];
   }
   for (int j=0;j<strlen(s2);j++){
       combined[strlen(s1)+j]=s2[j];
   }
   return combined;
}

int main(){
   char s1[100];
   char s2[100];
   char** combined = new char*;
   cout<<"Enter s1: "<<endl;
   cin.getline(s1,100);
   cout<<s1<<endl;
   cout<<"Enter s2: "<<endl;
   cin.getline(s2,100);
   my_str_cat(s1,s2,*combined);
   delete combined;
   
   return 0;
}

首先,它(会)return 一个值,只是没有分配给任何东西。这样做:

*combined = my_str_cat(s1,s2,*combined);

也要么按引用组合传递,要么全部不传递。

但这不会导致它因段错误而崩溃,因为您在未初始化 s1 时引用 s1[i]。 我将以 C 风格更正这一点,因为您的代码实际上只是带有 spice 的 C:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;
    
void my_str_cat( char* s1, char* s2, char* &combined){
    int len = strlen(s1);
    combined = (char*)calloc(1, (len + strlen(s2)));
    strcpy(combined, s1);
    strcpy(combined + len, s2);
}

int main(){
   char s1[100];
   char s2[100];
   char* combined;
   cout<<"Enter s1: " << endl;
   cin.getline(s1,100);
   cout<<s1<<endl;
   cout<<"Enter s2: " << endl;
   cin.getline(s2,100);
   my_str_cat(s1,s2, combined);
   cout << combined << endl;
   delete combined;
   
   return 0;
}

(虽然这有效,但我在写这篇文章时感觉遗漏了一些东西。如果你知道请告诉我它是什么。)

这个声明

char** combined = new char*;

声明了一个 char ** 类型的指针,它指向为 char *.

类型的对象分配的内存

在此调用中取消引用此指针

my_str_cat(s1,s2,*combined);

将类型为 char * 的未初始化指针传递给函数 my_str_cat

并且在函数内,这个未初始化的指针用于访问导致未定义行为的内存。

您需要为一个足够大的字符数组分配内存,以存储两个连接的 C 字符串,形成一个新的 C 字符串。

您可以在函数本身内分配内存。

函数如下面的演示程序所示。

#include <iostream>
#include <iomanip>
#include <cstring>

char * my_str_cat( const char *s1, const char *s2 )
{
    size_t n1 = std::strlen( s1 );
    
    char * result = new char[n1 + std::strlen( s2 ) + 1];
    
    std::strcpy( std::strcpy( result, s1 ) + n1, s2 );
    
    return result;
}

int main() 
{
    const size_t N = 100;
    char s1[N];
    char s2[N];
    
    std::cout << "Enter s1: ";
    
    std::cin.getline( s1, sizeof( s1 ) );
    
    std::cout << "Enter s2: ";
    
    std::cout << std::noskipws;
    
    std::cin.getline( s2, sizeof( s2 ) );
    
    char *combined = my_str_cat( s1, s2 );
    
    std::cout << combined << '\n';
    
    delete []combined;
    
    return 0;
}

程序输出可能看起来像

Enter s1: Hello
Enter s2:  World!
Hello World!

声明和定义函数的另一种方法是函数的用户负责将结果字符数组提供给将包含两个串联字符串的函数。

例如

#include <iostream>
#include <iomanip>
#include <cstring>

char * my_str_cat( char *result, const char *s1, const char *s2 )
{
    return std::strcat( std::strcpy( result, s1 ) , s2 );
}

int main() 
{
    const size_t N = 100;
    char s1[N];
    char s2[N];
    
    std::cout << "Enter s1: ";
    
    std::cin.getline( s1, sizeof( s1 ) );
    
    std::cout << "Enter s2: ";
    
    std::cout << std::noskipws;
    
    std::cin.getline( s2, sizeof( s2 ) );
    
    char *combined = new char[std::strlen( s1 ) + std::strlen( s2 ) + 1];

    std::cout << my_str_cat( combined, s1, s2 ) << '\n';
    
    delete []combined;
    
    return 0;
}

程序输出可能与上一个演示程序

的显示方式相同
Enter s1: Hello
Enter s2:  World!
Hello World!

在不使用标准字符串函数且仅使用循环的情况下,可以按以下方式定义函数。

char * my_str_cat( char *result, const char *s1, const char *s2 )
{
    char *p = result;
    
    while ( *s1 ) *p++ = *s1++;
    
    while ( ( *p++ = *s2++ ) );

    return result;
}