Thread: I need help to this

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    I need help to this

    Here is my code

    Code:
    #include<stdio.h> 
    #define MAXL 256 
     
    // funcoes pos e inv 
     
    int mystrlen(char *string) { // DEVOLVE TAMANHO DA STRING 
        int tamanho = 0;  
        while(*(string+tamanho)!='\0') { 
          tamanho++; 
        } 
        return tamanho; 
    } 
     
    char* inv(char* C) { // INVERTER 
        int length, i; 
        char *begin; // Apontador para o inicio da string 
        char *end; // Apontador para o fim da string 
        char temp;  
        length = mystrlen(C); // Chamada a função strlen que devolve o tamanho da string 
        begin = C;              
        end = C;             
        end += length-1;     
        for (i=0;i<length/2;i++) {         
            temp = *end; 
            *end = *begin; 
            *begin = temp; 
            begin++; 
            end--; 
        } 
        return C; 
    } 
     
    char* pos(char* C,char* S) { // PROCURAR C em S 
        int CAux = 0; int csize = mystrlen(C); 
        int SAux = 0; int ssize = mystrlen(S); 
        int howMany = 1; 
        char results[ssize];
        char* r = results; 
        int i;  
        for(i = 0;i < ssize; i++){ 
            if(C[CAux] == S[i]){ 
                SAux = i; 
                while((C[CAux] == S[SAux]) && (CAux < csize-1)){ 
                    CAux++; 
                    SAux++; 
                    howMany++; 
                    if(howMany == csize-1){ 
                        sprintf(results, " %d", i); 
                    } 
                } 
                SAux = i + 1; 
                CAux = 0; 
                howMany = 0; 
            } 
        } 
        return r; 
    } 
     
     
    /* _-*_-*_-*_-*_-* MAIN *-_*-_*-_*-_*-_ */ 
     
    int main(int argc, char *argv[]) {     
        char str[MAXL]; 
        char *r=str;     
        // declarar aqui outras variaveis, se necessario 
        char* compStr;
        char* res; 
        while(r!=NULL) { 
            r=fgets(str,MAXL,stdin); 
     
            // chamar a funcao pos() ou inv(), conforme o caso 
            if(argv[1][0] == 'p') { // pos() 
                compStr = argv[2];            
                res = pos(r, compStr); 
            }     E
            else if(argv[1][0] == 'i') { // inv()
                res = inv(r);
                //printf("%s\n", res); 
            } 
             
            // imprimir os resultados 
            printf("%s\n",res);         
        } 
        return 0; 
    }
    on running this program, if you do ./cadeias i: (inv function that reverses C)
    you will able to write a string to returns the reverse.

    ex: ./cadeias i (enter)
    Pedro
    out -> ordeP

    if you do ./cadeias p anystring: (pos function that finds C in S)
    you will be able to write a sting to find in "anystring" and returns the posicions that the sub string are.

    ex: ./cadeias p PETER
    E
    out -> 2 4

    but, its printing things like that: "pYS�����"

    can you help me finding where is the problem???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char results[ssize];
    > char* r = results;
    ...
    > return r;
    You're returning a pointer to a local variable.
    So when the function returns, the variable goes out of scope, the pointer is invalid, and the memory is trashed by doing something else.

    Try something like this, where you pass in a pointer to where you want the result stored.
    Code:
        char res[100];
    ...
       pos(r, compStr. res);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    res = pos(compStr, r); i forgot to rewrite on my code


    I'm doing that because pos function recives 2 char pointers char * C and char * S

    how can i turn this compatible?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well typically, you would use malloc to allocate a variable amount of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    NEW VERSION OF MY CODE.

    Code:
    #include<stdio.h> 
    #define MAXL 256 
     
    // funcoes pos e inv 
     
    int mystrlen(char *string) { // My String Lenght - returns string lenght 
        int len = 0;  
        while(*(string+len)!='\0') { 
          len++; 
        } 
        return len; 
    } 
     
    char* inv(char* C) { // REVERSE C and returns C reversed 
        int length, i; 
        char *begin; 
        char *end; 
        char temp;  
        length = mystrlen(C); 
        begin = C;              
        end = C;             
        end += length-1;     
        for (i=0;i<length/2;i++) {         
            temp = *end; 
            *end = *begin; 
            *begin = temp; 
            begin++; 
            end--; 
        } 
        return C; 
    } 
     
    char* pos(char* C,char* S) { // FIND C in S and returns a pointer for a char that contains the positions where C is in S 
        int CAux = 0; int csize = mystrlen(C); 
        int SAux = 0; int ssize = mystrlen(S); 
        int howMany = 1; 
        char results[ssize];
        char* r = results; 
        int i;  
        for(i=0;i<ssize;i++){ 
            if(C[CAux] == S[i]){ 
                SAux = i; 
                while((C[CAux]==S[SAux]) && (CAux<(csize-1))){ 
                    CAux++; 
                    SAux++; 
                    howMany++; 
                    if(howMany == (csize-1)){ 
                        sprintf(results + mystrlen(results), " %d", i); 
                    } 
                } 
                SAux = i + 1; 
                CAux = 0; 
                howMany = 0; 
            }
             
        } 
        return r; 
    } 
     
     
    /* _-*_-*_-*_-*_-* MAIN *-_*-_*-_*-_*-_ */ 
     
    int main(int argc, char *argv[]) {     
        char str[MAXL]; 
        char *r=str;     
        // declarar aqui outras variaveis, se necessario (another var.) 
        char *compStr;
        char* res; 
        while(r!=NULL) { 
            r=fgets(str,MAXL,stdin);
     
            // chamar a funcao pos() ou inv(), conforme o caso (call pos() or inv()) 
            if(argv[1][0] == 'p') { // pos() 
                compStr = argv[2];            
                res = pos(compStr, r); 
            }     
            else if(argv[1][0] == 'i') { // inv()
                res = inv(r); 
            } 
             
            // imprimir os resultados (Print results) 
            printf("%s\n",res);         
        } 
        return 0; 
    }
    on running this program:

    1 ____ "i"

    if you do ./cadeias i: (inv function that reverses C)
    you will able to write a string to return the reverse.

    ex: ./cadeias i (enter)
    Pedro
    out -> ordeP

    2 _____ "p"

    if you do ./cadeias p anystring: (pos function that finds C in S)
    you will be able to write a sting to find in "anystring" and returns the posicions that the sub string are.

    ex: ./cadeias p PETER
    E
    out -> 2 4

    but, its printing things like that: "pYS�����"

    can you help me finding where is the problem???
    Last edited by PoisonMind; 03-25-2013 at 02:08 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Nope - I looks for malloc, couldn't see it.
    Thanks for playing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    -_-

    o_o

    o_O

    Bwahaha!

    Did that really just happen? (*)

    [Edit]
    Responding to edited post:

    If you want help you'll have to be more responsible with volunteer time.

    If you don't understand what Salem has suggested feel free to ask, but posting different code with the exact same problem isn't going to win anyone over.

    Try to employ what you've been told.
    [/Edit]

    This is now my favorite thread.

    Soma

    (*) It seems to me like he tried to hijack this thread not realizing it was his own.
    Last edited by phantomotap; 03-25-2013 at 02:18 PM.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    ????? this is an help???

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You will get answers like that when you do not make any effort to revise a previous post, or ask new questions / try new things based on the information somebody has volunteered to you.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    sorry salem i dont see your post for malloc.

    I tried this

    Code:
    char* pos(char* C,char* S) { // FIND C in S and returns a pointer for a char that contains the positions where C is in S 
        int CAux = 0; int csize = mystrlen(C); 
        int SAux = 0; int ssize = mystrlen(S); 
        int howMany = 1; 
        char results[ssize];
        char* r = malloc(sizeof(char)*1024); 
        int i;  
        for(i=0;i<ssize;i++){ 
            if(C[CAux] == S[i]){ 
                SAux = i; 
                while((C[CAux]==S[SAux]) && (CAux<(csize-1))){ 
                    CAux++; 
                    SAux++; 
                    howMany++; 
                    if(howMany == (csize-1)){   
                        sprintf(r + mystrlen(r), " %d", i);
                        printf("%s\n",r);     
                    } 
                } 
                SAux = i + 1; 
                CAux = 0; 
                howMany = 0; 
            }
             
        }
        free(r); 
        return r; 
    }
    and gcc give me:

    pedro@pedro:~/Documentos/AC/T2$ gcc -Wall -o cadeias cadeias.c
    cadeias.c: Na função ‘pos’:
    cadeias.c:38:2: aviso: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
    cadeias.c:38:12: aviso: incompatible implicit declaration of built-in function ‘malloc’ [habilitado por padrão]
    cadeias.c:58:2: aviso: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
    cadeias.c:58:2: aviso: incompatible implicit declaration of built-in function ‘free’ [habilitado por padrão]

    Am i using malloc in bad context???

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No you didn't include the required header file (stdlib.h).

    Jim

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    free(r); 
    return r;
    It's not very clever to free a pointer before returning it.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed