如何在main函数中打印字符数组的内容,其函数通过字符指针返回其基址?

时间:2022-02-14 01:45:16

I was trying to print the contents of a character array in the main function by accepting the base address of that array in a character pointer which is returned by another function. Actually here the code is to accept a string with vowels and return the modified string without vowels. But when i print the character array in the main function, the code does not print anything. It is possible to do with a string class though but i want to know what is the problem while using character pointers.

我试图通过在另一个函数返回的字符指针中接受该数组的基地址来在main函数中打印字符数组的内容。实际上这里的代码是接受带元音的字符串并返回没有元音的修改后的字符串。但是当我在main函数中打印字符数组时,代码不会打印任何内容。虽然可以使用字符串类,但我想知道在使用字符指针时有什么问题。

    #include<iostream>
    #include<string.h>
    using namespace std;
    char * print(char * s)
    {
        int j=0,len;
        while(s[j]!='\0')
           j++;
        len=j;
        char scopy[len];
        char * sn;
        j=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]!='a' && s[i]!='e' && s[i]!='i' && s[i]!='o' && s[i]!='u' && s[i]!='A' && s[i]!='E' && s[i]!='I' && s[i]!='O' && s[i]!='U')
            {
                scopy[j]=s[i];
                j++;
            }
        }
        sn=scopy;
        return sn;
    }
    int main()
    {
        int n;
        cout<<"enter n:\n";
        cin>>n;
        char st[n];
        cout<<"enter string:\n";
        cin>>st;
        char * ps=print(st);
        cout<<"the string is\n"<<ps[0]<<ps[1]<<ps[2]<<"\n";  //Here when i print the array contents individually using the base pointer, it prints successfully
        for(int i=0;ps[i]!=NULL;i++)  //the problem is here, it does not print anything
           cout<<ps[i];
    } 

1 个解决方案

#1


0  

Your code invokes Undefined Behavior, because it returns a pointer to a local variable, which will go out of scope as soon as the function will terminate:

您的代码调用Undefined Behavior,因为它返回一个指向局部变量的指针,该函数将在函数终止后超出范围:

char * print(char * s)
{
    ...      
    char scopy[len];
    char * sn;
    ...
    sn=scopy;
    return sn;
}

Moreover, you do not null-terminate the string, and then you do:

此外,您不会终止字符串,然后您执行以下操作:

for(int i=0; ps[i]!=NULL ;i++)
     cout << ps[i];

ps is not null terminated, thus ps[i] != NULL will be always true.

ps不是以null结尾,因此ps [i]!= NULL将始终为true。


Since you are using C++, I strongly recommend using std::string, and not C-style strings.

由于您使用的是C ++,我强烈建议您使用std :: string,而不是C风格的字符串。

#1


0  

Your code invokes Undefined Behavior, because it returns a pointer to a local variable, which will go out of scope as soon as the function will terminate:

您的代码调用Undefined Behavior,因为它返回一个指向局部变量的指针,该函数将在函数终止后超出范围:

char * print(char * s)
{
    ...      
    char scopy[len];
    char * sn;
    ...
    sn=scopy;
    return sn;
}

Moreover, you do not null-terminate the string, and then you do:

此外,您不会终止字符串,然后您执行以下操作:

for(int i=0; ps[i]!=NULL ;i++)
     cout << ps[i];

ps is not null terminated, thus ps[i] != NULL will be always true.

ps不是以null结尾,因此ps [i]!= NULL将始终为true。


Since you are using C++, I strongly recommend using std::string, and not C-style strings.

由于您使用的是C ++,我强烈建议您使用std :: string,而不是C风格的字符串。