如何从函数返回可变大小的字符串? [英] How do I return a variable size string from a function?

查看:89
本文介绍了如何从函数返回可变大小的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数的工作代码,该函数将返回具有随机长度的随机字符串.

I need a working code for a function that will return a random string with a random length.

下面的代码会更好地描述我想做的事情.

What I want to do would be better described by the following code.

char *getRandomString()
{
    char word[random-length];
    // ...instructions that will fill word with random characters.
    return word;
}
void main()
{
    char *string = getRandomString();
    printf("Random string is: %s\n", string);
}

为此,我严格禁止使用除 stdio.h 以外的任何其他包含. 该项目将适合为PIC单片机进行编译,因此我不能使用malloc()或类似的东西. 之所以在这里使用 stdio.h 是为了让我能够使用 GCC 检查输出.

For this, I am strictly forbidden to use any other include than stdio.h. This project will be adapted to be compiled for a PIC Microcontroller, hence I cannot use malloc() or such stuff. The reason why I use stdio.h here, is for me to be able to inspect the output using GCC.

当前,此代码给出了此错误.-
警告:函数返回本地变量的地址(默认情况下启用)"

Currently, this code gives this error.-
"warning: function returns address of local variable [enabled by default]"

然后,我认为这可以解决问题.-

Then, I thought this could work.-

char *getRandomString(char *string)
{
    char word[random-length];
    // ...instructions that will fill word with random characters.
    string = word;
    return string;
}
void main()
{
    char *string = getRandomString(string);
    printf("Random string is: %s\n", string);
}

但是它只打印一堆废话字符.

But it only prints a bunch of nonsense characters.

推荐答案

共有三种常见的方法.

  1. 让调用者将指针与长度参数一起传递到要在其中存储数据的数组(数组的第一个元素).如果要返回的字符串大于传入的长度,则为错误;您需要决定如何处理它. (您可以截断结果,或者可以返回空指针.无论哪种方式,调用者都必须能够处理它.)

  1. Have the caller pass in a pointer to (the first element of) an array into which the data is to be stored, along with a length parameter. If the string to be returned is bigger than the passed-in length, it's an error; you need to decide how to deal with it. (You could truncate the result, or you could return a null pointer. Either way, the caller has to be able to deal with it.)

返回一个指向新分配的对象的指针,使调用者有责任在完成后调用free.如果malloc()失败,则可能返回一个空指针(这总是有可能的,您应该总是 检查它).由于mallocfree是在<stdlib.h>中声明的,因此不符合您的(人工)要求.

Return a pointer to a newly allocated object, making it the caller's responsibility to call free when done. Probably return a null pointer if malloc() fails (this is always a possibility, and you should always check for it). Since malloc and free are declared in <stdlib.h> this doesn't meet your (artificial) requirements.

返回一个指向 static 数组(第一个元素)的指针.这避免了将指针返回到本地分配的对象的错误,但是它有其自身的缺点.这意味着以后的调用会破坏原始结果,并施加一个固定的最大大小.

Return a pointer to (the first element of) a static array. This avoids the error of returning a pointer to a locally allocated object, but it has its own drawbacks. It means that later calls will clobber the original result, and it imposes a fixed maximum size.

如果这些是理想的解决方案,那就没有了.

None if these is an ideal solution.

这篇关于如何从函数返回可变大小的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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