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

时间:2022-03-09 17:26:34

I want to return a character array from a function. Then I want to print it in main. how can I get the character array back in main function?

我想从函数中返回一个字符数组。然后我想把它打印出来。如何在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;
}

Please tell me what am I doing wrong?

请告诉我我做错了什么?

3 个解决方案

#1


7  

#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);
    return 0;
}

This will compile fine without any warning

这将在没有任何警告的情况下编译良好

  1. #include stdlib.h
  2. # include stdlib.h
  3. pass test=substring(i,j,s);
  4. 通过测试=子串(i,j s);
  5. remove m as it is unused
  6. 删除未使用的m
  7. either declare char substring(int i,int j,char *ch) or define it before main
  8. 要么声明char子字符串(int i,int j,char *ch),要么在main之前定义它

#2


4  

Daniel is right: http://ideone.com/kgbo1C#view_edit_box

丹尼尔是对的:http://ideone.com/kgbo1C view_edit_box

Change

改变

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

to

test=substring(i,j,s);  

Also, you need to forward declare substring:

此外,您还需要转发declare子字符串:

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

int main // ...

#3


3  

Lazy notes in comments.

懒惰的评论中指出。

#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;
}

#1


7  

#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);
    return 0;
}

This will compile fine without any warning

这将在没有任何警告的情况下编译良好

  1. #include stdlib.h
  2. # include stdlib.h
  3. pass test=substring(i,j,s);
  4. 通过测试=子串(i,j s);
  5. remove m as it is unused
  6. 删除未使用的m
  7. either declare char substring(int i,int j,char *ch) or define it before main
  8. 要么声明char子字符串(int i,int j,char *ch),要么在main之前定义它

#2


4  

Daniel is right: http://ideone.com/kgbo1C#view_edit_box

丹尼尔是对的:http://ideone.com/kgbo1C view_edit_box

Change

改变

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

to

test=substring(i,j,s);  

Also, you need to forward declare substring:

此外,您还需要转发declare子字符串:

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

int main // ...

#3


3  

Lazy notes in comments.

懒惰的评论中指出。

#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;
}