如何从C中的函数返回字符数组

如何从C中的函数返回字符数组,c,C,我想从函数返回一个字符数组。然后我想在main中打印它。如何在main函数中获取字符数组 #include<stdio.h> #include<string.h> int main() { int i=0,j=2; char s[]="String"; char *test; test=substring(i,j,*s); printf("%s",test); return 0; } char *substrin

我想从函数返回一个字符数组。然后我想在
main
中打印它。如何在
main
函数中获取字符数组

#include<stdio.h>
#include<string.h>
int main()
{
    int i=0,j=2;
    char s[]="String";
    char *test;

    test=substring(i,j,*s);   
    printf("%s",test);
    return 0;
}


char *substring(int i,int j,char *ch)
{
    int m,n,k=0; 
    char *ch1;
    ch1=(char*)malloc((j-i+1)*1);
    n=j-i+1;

    while(k<n)
    {   
        ch1[k]=ch[i];
        i++;k++;
    }   

    return (char *)ch1;
}
#包括
#包括
int main()
{
int i=0,j=2;
字符s[]=“字符串”;
字符*测试;
测试=子串(i,j,*s);
printf(“%s”,测试);
返回0;
}
char*子字符串(inti,intj,char*ch)
{
int m,n,k=0;
char*ch1;
ch1=(char*)malloc((j-i+1)*1);
n=j-i+1;
而(kDaniel是对的:

改变

test=substring(i,j,*s);

此外,还需要向前声明子字符串:

char *substring(int i,int j,char *ch);

int main // ...

注释中的惰性注释

#include <stdio.h>
// for malloc
#include <stdlib.h>

// you need the prototype
char *substring(int i,int j,char *ch);


int main(void /* std compliance */)
{
  int i=0,j=2;
  char s[]="String";
  char *test;
  // s points to the first char, S
  // *s "is" the first char, S
  test=substring(i,j,s); // so s only is ok
  // if test == NULL, failed, give up
  printf("%s",test);
  free(test); // you should free it
  return 0;
}


char *substring(int i,int j,char *ch)
{
  int k=0;
  // avoid calc same things several time
  int n = j-i+1; 
  char *ch1;
  // you can omit casting - and sizeof(char) := 1
  ch1=malloc(n*sizeof(char));
  // if (!ch1) error...; return NULL;

  // any kind of check missing:
  // are i, j ok? 
  // is n > 0... ch[i] is "inside" the string?...
  while(k<n)
    {   
      ch1[k]=ch[i];
      i++;k++;
    }   

  return ch1;
}
#包括
//马洛克
#包括
//你需要原型
char*子字符串(inti,intj,char*ch);
int main(无效/*标准符合性*/)
{
int i=0,j=2;
字符s[]=“字符串”;
字符*测试;
//s指向第一个字符,s
//*s“是”第一个字符,s
test=substring(i,j,s);//所以只有s是可以的
//如果test==NULL,失败,则放弃
printf(“%s”,测试);
释放(测试);//你应该释放它
返回0;
}
char*子字符串(inti,intj,char*ch)
{
int k=0;
//避免多次计算相同的事情
int n=j-i+1;
char*ch1;
//您可以省略casting和sizeof(char):=1
ch1=malloc(n*sizeof(char));
//如果(!ch1)错误…;返回NULL;
//任何类型的支票丢失:
//我还好吗,j?
//n>0…ch[i]在字符串中吗?。。。
而(k
#包括
#包括
#包括
char*子字符串(inti,intj,char*ch)
{
int n,k=0;
char*ch1;
ch1=(char*)malloc((j-i+1)*1);
n=j-i+1;

while(kdo)你有错误吗?你的编译器应该告诉你
test=substring(i,j,*s);
你正在取消对
s的引用,而你不应该这样做。我收到一条错误消息:“substring”的类型冲突与错误无关,但我相信你的
(char*)
强制转换是不必要的,1的乘法是没有意义的-
(j-i+1)*1
应该简化为
j-i+1
。除了其他注释外,您需要在
substring
中循环末尾的
ch1
中添加一个空终止符,以获得整数类型数组的正确结果。但是,当我尝试使用字符数组时,我只得到了错误,它将在没有任何警告的情况下正常编译//1.>包含stdlib.h//2.>通过测试=子字符串(i,j,s);//3.>删除未使用的m//4.>声明字符子字符串(int i,int j,char*ch)或在main之前定义它是否只有我在
测试中看到内存泄漏?(2,5年后;)
#include <stdio.h>
// for malloc
#include <stdlib.h>

// you need the prototype
char *substring(int i,int j,char *ch);


int main(void /* std compliance */)
{
  int i=0,j=2;
  char s[]="String";
  char *test;
  // s points to the first char, S
  // *s "is" the first char, S
  test=substring(i,j,s); // so s only is ok
  // if test == NULL, failed, give up
  printf("%s",test);
  free(test); // you should free it
  return 0;
}


char *substring(int i,int j,char *ch)
{
  int k=0;
  // avoid calc same things several time
  int n = j-i+1; 
  char *ch1;
  // you can omit casting - and sizeof(char) := 1
  ch1=malloc(n*sizeof(char));
  // if (!ch1) error...; return NULL;

  // any kind of check missing:
  // are i, j ok? 
  // is n > 0... ch[i] is "inside" the string?...
  while(k<n)
    {   
      ch1[k]=ch[i];
      i++;k++;
    }   

  return ch1;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *substring(int i,int j,char *ch)
{
    int n,k=0;
    char *ch1;
    ch1=(char*)malloc((j-i+1)*1);
    n=j-i+1;

    while(k<n)
    {
        ch1[k]=ch[i];
        i++;k++;
    }

    return (char *)ch1;
}

int main()
{
    int i=0,j=2;
    char s[]="String";
    char *test;

    test=substring(i,j,s);
    printf("%s",test);
    free(test); //free the test 
    return 0;
}