2013-05-08 31 views
9

'daki bir işlevden döndürme Bir işlevden bir karakter dizisi döndürmek istiyorum. Sonra onu main'da yazdırmak istiyorum. Karakter dizisini main işlevine nasıl geri döndürebilirim?Bir char dizisini 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; 
} 

Lütfen bana neyi yanlış yaptığımı söyle?

+0

Eğer herhangi bir hata yapmadan önce bunu tanımlamak kullanılmayan olduğu gibi

  • m kaldırmak ?? –

  • +3

    Derleyiciniz size şunu söylemeliydi: 'test = substring (i, j, * s); –

    +0

    Bir hata mesajı alıyorum: 'substring' – sayan

    cevap

    4

    Daniel haklı: http://ideone.com/kgbo1C#view_edit_box

    Değişim

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

    Ayrıca

    test=substring(i,j,s); 
    

    için, size alt dize bildirmek iletmek gerekir:

    char *substring(int i,int j,char *ch); 
    
    int main // ... 
    
    +0

    tamsayı tipi dizi için doğru sonuç elde etme. Ama aynı zamanda bir char dizisi ile aynı hata denediğimde ben sadece – sayan

    +0

    hata aldım Ben de buna göre kodu değiştirdim ama aynı hata alıyorum ... – sayan

    3

    Tembel notu yorumlarda.

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

    Bu, herhangi bir uyarı

    1. #include stdlib.h
    2. geçiş test=substring(i,j,s) olmadan iyi derlenir; o
    3. ya char substring(int i,int j,char *ch) beyan veya ana
    +0

    herhangi bir uyarı olmadan iyi derleyecektir //1.>include stdlib.h //2.>pass testi = substring (i, j, s); //3.> Kullanılmadıkça m'yi değiştirin //4.> char substring'i (int i, int j, char * ch) belirtin veya ana –

    +0

    'dan önce bunu gerçekten yardımcı olun. – MELWIN

    +0

    welcome! :-) @MELWIN –

    İlgili konular