从函数返回char *不起作用 [英] Returning char* from function not working

查看:178
本文介绍了从函数返回char *不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual studio c ++显示第24行的字符串有一个数组元素,但顶部包含用户输入的所有文本。但是当我发送到PutString(),它消失。为什么?

Visual studio c++ shows that "string" on line 24 has one array element, but the top contains all the text that was input by the user. But when I send to PutString(), it disappears. Why?

#include <stdio.h>

void PutString( const char* pChar ){
    for( ; *pChar != 0; pChar++ )
    {
        putchar( *pChar );
    }
}

char* GetString(){
    char c[100];
    int i = 0;
    do
    {
        c[i] = getchar();
    }while( c[i++] != '\n' );
    c[i] = '\0';
    // PutString( c );
    return c;
}

void main(){
    char* string = GetString();
    PutString( string );
}


推荐答案

> c 是 GetString 内的局部变量,您正在返回其地址,之后它将消失(移出范围)。这是未定义的行为 - 你不允许使用超出范围的东西。事实上,如果你通过 gcc 运行它,它会明确地告诉你:

Because c is a local variable within GetString and you are returning its address, after which it disappears (moves out of scope). That's undefined behaviour - you're not allowed to use stuff that's gone out of scope. In fact, if you run that through gcc, it tells you that quite explicitly:

qq.cpp:9: warning: address of local variable ‘c’ returned

你想从一个函数返回非简单的东西(例如数组而不是整数或浮点数,它为你创建一个副本),你需要(例如)动态分配它,以便它存活的函数返回。类似更改的内容:

If you want to return non-simple things (such as arrays instead of integers or floats, which make a copy for you) from a function, you need to (for example) dynamically allocate it so that it survives function return. Something like changing:

char c[100];

into:

char *c = malloc (100); // or equivalent 'new'.

(当然还记得要释放/删除它)。

(and remembering to free/delete it eventually, of course).

即使您可以在 GetString 中返回时在调试器中查看,但仍然无法保证按照标准。

Even though you may see it in the debugger when you return from GetString, it's still not guaranteed as per the standards.

在任何情况下,下一个堆栈操作(如调用 PutString

In any case, there's a good chance the next stack manipulation (such as calling PutString) will wipe out the information.

这里是一个C ++版本,它是 new / delete 方式:

Here's a C++ version that does it the new/delete way:

#include <stdio.h>

void PutString (const char* pChar ){
    for (; *pChar != 0; pChar++)
        putchar( *pChar );
}

char* GetString (void) {
    char *c = new char[100];      // memory behind c will survive ...
    int i = 0;
    do {
        c[i] = getchar();
    } while (c[i++] != '\n');
    c[i] = '\0';
    return c;
}                                 // ... past here ...

int main (void) {
    char* string = GetString();
    PutString (string);
    delete[] string;              // ... until here.
    return 0;
}






可能有更好的方法在C( fgets )中查看基于行的输入或查看< a href =http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921>我之前的答案)和C ++(例如字符串getline char array getline )。


I should also mention that there are probably better way to get line-based input in both C (fgets or see an earlier answer of mine) and C++ (such as the string getline and the char array getline).

这篇关于从函数返回char *不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆