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
#include stdlib.h
- pass
test=substring(i,j,s)
; - remove
m
unused - either declare
char substring(int i,int j,char *ch)
or define before main
Comments
Post a Comment