2013-05-08 13 views
9

Voglio restituire un array di caratteri da una funzione. Quindi voglio stamparlo in main. Come posso riportare l'array di caratteri nella funzione main?come restituire un array di caratteri da una funzione in C

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

Per favore dimmi cosa sto sbagliando?

+0

cosa si ottiene eventuali errori ?? –

+3

Il tuo compilatore avrebbe dovuto dirti che 'test = sottostringa (i, j, * s);' stai dereferenziando 's' quando non dovresti. –

+0

viene visualizzato un messaggio di errore: tipi in conflitto per "sottostringa" – sayan

risposta

4

Daniel ha ragione: http://ideone.com/kgbo1C#view_edit_box

Change

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

a

test=substring(i,j,s); 

Inoltre, è necessario inoltrare dichiarare la stringa:

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

int main // ... 
+0

im ottenere il risultato giusto per l'array di tipo intero. Ma quando sto provando lo stesso con un array di matrice im ottenere l'errore solo – sayan

+0

Ho cambiato il codice di conseguenza, ma ricevendo lo stesso errore ... – sayan

3

nota pigro s nei commenti.

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

Questo compilerà bene senza alcun preavviso

  1. #include stdlib.h
  2. passaggio test=substring(i,j,s);
  3. rimuovere m in quanto è inutilizzato
  4. sia dichiarano char substring(int i,int j,char *ch) o definire prima principale
+0

si compila bene senza alcun avviso //1.>include stdlib.h //2.>pass test = sottostringa (i, j, s); //3.> rimuovi m perché non è usato //4.> o dichiara la sottostringa di carattere (int i, int j, char * ch) o definiscila prima del principale –

+0

veramente utile. – MELWIN

+0

benvenuto! :-) @MELWIN –

Problemi correlati