comment retourner un tableau de caractères d'une fonction dans C
Je souhaite retourner un tableau de caractères à partir d'une fonction. Alors je veux l'imprimer en main
. comment puis-je obtenir le tableau de caractères de retour dans main
fonction?
#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;
}
S'il vous plaît dites-moi ce que je fais mal?
source d'informationauteur sayan
Vous devez vous connecter pour publier un commentaire.
Cette compile très bien sans aucun avertissement
#include stdlib.h
test=substring(i,j,s)
;m
comme il est inutiliséchar substring(int i,int j,char *ch)
ou les définir avant les principauxDaniel a raison: http://ideone.com/kgbo1C#view_edit_box
Changement
à
Aussi, vous devez déclarer avant substring:
Paresseux notes dans les commentaires.