返回从函数的字符串

返回从函数的字符串

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

问题描述

我试图让c上一个子功能。它必须返回CDEF,但它没有返回值。我该如何解决呢?谢谢你。

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&string.h中GT;字符*字符串(字符*,INT,INT);诠释主(){
    焦炭的东西[] =abcdefghjklmnoprstuvyz;
    的printf(%S \\ N的东西);
    的printf(%S \\ n,子串(东东,2,6));    残培();
    返回0;
}字符*字符串(字符*文本,诠释一个,INT B){
    字符n文字[B-A]。
    焦炭TMP [2];
    的strcpy(n文字,);
    的for(int i为一个; I< B;我++){
        TMP [0] =文[I]
        TMP [1] ='\\ 0';
        strcat的(n文字,TMP);
    }
    返回n文字;
}


解决方案

您正在一个指针返回,可能无法在函数返回之后存在的变量的错误。您需要在调用函数分配空间,只是把结果提供的空间,或者用静态创建功能永久空间。注 - 乔纳森莱弗勒指出的 - 因为空间是永久,你不能块的长度从一个呼叫切换到下了,你就必须选择那些<$一个明智的值与测试C $ C> b-A + 1 不超过分配的空间更长。因此,我的第二个方法更强劲。

 的char *字符串(字符*文本,诠释一个,INT B){
    静态字符n文字[100];
    如果((B-A + 1)→100)//做点什么!你不能复制此!
    // code
    返回n文字;
}

合共雇用俄罗斯指出,以这种方式使用静态在任何情况下,是相当危险的,因为另一块code,而你还在使用第一个调用的结果可能会调用这个函数。如果你做任何多线程这是不可取的,但它是一个快速解决方案,如果你有一个线程。

一个更好的配方

 无效子(字符*文本,诠释一个,INT B,字符* n文字){
    // code,没有返回
}

在后一种情况下,您创建调用函数空间和指针传递给。 n您的主程序你会

 字符ShortString短[100];
子(东东,4,6,ShortString短);
的printf(%S \\ n,ShortString短);

顺便说一句,你的复制子的方法是非常低效的。考虑替换它

 的for(int i为一个; I&LT; B;我++)n文字[I-A] =文[I]
n文字[B-A] ='\\ 0';

从这里就可以看出,你确实需要分配 n文字[B-A + 1] 元素,否则有对最后的'\\ 0'。

I'm trying to make a substring function on c. It must be return "cdef", but it returns nothing. How can i fix it? Thanks.

#include<stdio.h>
#include<conio.h>
#include<string.h>

char* substring( char *, int, int );

int main(){
    char stuff[] = "abcdefghjklmnoprstuvyz";
    printf("%s\n", stuff);
    printf("%s\n", substring(stuff, 2, 6));

    getch();
    return 0;
}

char* substring(char *text, int a, int b){
    char nText[b-a];
    char tmp[2];
    strcpy(nText, "");
    for(int i=a; i<b; i++){
        tmp[0] = text[i];
        tmp[1] = '\0';
        strcat(nText, tmp);
    }
    return nText;
}
解决方案

You are making the mistake of returning a pointer to a variable that may not exist after the function returns. You need to allocate the space in the calling function and just put the result in the space provided, or create permanent space in the function with static. Note - as pointed out by Jonathan Leffler - since the space is "permanent", you can't change the length of the block from one call to the next, and you would have to pick a "sensible" value and test that b-a+1 is not longer than the space allocated. Thus my second method is more robust.

char* substring(char *text, int a, int b){
    static char nText[100];
    if ((b-a+1)>100) // do something! you can't copy this!
    // code
    return nText;
}

As Employed Russian pointed out, using a static in this way is in any case quite dangerous since another piece of code might call this function while you're still using the result of the first call. This is NOT ADVISABLE if you do any kind of multi threading, but it's a quick fix if you have a single thread.

A better formulation is

void substring(char *text, int a, int b, char *nText) {
    // code, nothing to return
}

In the latter case, you create space in the calling function and pass the pointer to substring. n your main program you would have

char shortString[100];
substring(stuff, 4, 6, shortString);
printf("%s\n", shortString);

As an aside, your method for copying the substring is terribly inefficient. Consider replacing it with

for(int i=a; i<b;i++) nText[i-a]=text[i];
nText[b-a] = '\0';

From this you can see that you actually need to allocate nText[b-a+1] elements, otherwise there is no space for the final '\0'.

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

08-15 04:33