how to return a char array from a function in C -


i want return character array function. want print in main. how can character array in main function?

#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 doing wrong?

#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 compile fine without warning

  1. #include stdlib.h
  2. pass test=substring(i,j,s);
  3. remove m unused
  4. either declare char substring(int i,int j,char *ch) or define before main

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -