Thread: Error in C

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Error in C

    Good afternoon everyone. I wanted to break the resolve of a program but I have an error and I can not fix it wanted to know your opinion about it. I'll pass the sentence and then the code for you to know I did.

    Statement:
    Implement strsubst without using library functions.
    char * strsubst(char * s, const char * old_s, const char * new_s);

    This function replaces the first occurrence of string s a old_s by new_s. The strings and old_s new_s may have different lengths. It is assumed that the memory space pointed to by s is sufficient to store the final result. The function returns the pointer s if you made the substitution s or NULL if not contains old_s.


    Code:
    #include <stdio.h>
    
    char * strsubst(char * s, const char * old_s, const char * new_s)
    {
    char *aux=NULL;
    int tamanhostring=40; // tamanho da string s;
    int tamanho1=20; // tamanho da string old_s;
    int i,j;
    
    for(i=0; i <= tamanhostring; i++)
        if(s[i] == *old_s)
            *aux = i;
    
    if(*aux)
    {
    for(j=0; j<tamanho1;j++)
        s[(aux-s) + j] = new_s[j];
        return (s);
    }
    else
    return NULL;
    }
    
    int main()
      {
        char string[40],antiga[20],nova[20];
    
        printf("\n");
        printf("Entre com a string :");
        gets(string);
        printf("\n");
        printf("Entre com a antiga palavra :");
        gets(antiga);
        printf("\n");
        printf("Entre com a nova palavra :");
        gets(nova);
        printf("\n");
        //printf("nova string ==> %s\n",strsubst(string,antiga,nova));
    
        return(0);
    }

    Error:

    Segmentation fault

    Solution:
    Well I've tried various solutions but can not find where the error in the main is he does not get to write the last printf is to comment so that the error must lie on the other method has changed many things but it does not. He wanted to know your opinion and what I should change my program to attain the objectives.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    tamaņo del Array!

    Code:
    for(i=0; i <= tamanhostring; i++)
    it might be that you are writing or reading beyond the bounds of your array,

    you have written " <= ", i have not gone through your code properly but this would usually be " < " because the array index starts at 0.
    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'"

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    For starters, don't ever use gets(). Use fgets() instead.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    you should send the size of the strings as a parameter.

    also in the first loop, is probably not doing what you think, you're comparing a single character, *old_s(and it is always the same), against a single character of s[i], but then you're saving i,
    which is an int variable.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    if(*aux)
    Might this have to be, I am not certain right now; feeling sick my thinking might be effected.

    Code:
    if(aux)
    Tim S.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The fact that you are trying to write to an address that points to NULL (aux) is definitely going to give you a seg fault.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Error In C

    The problem continues to work Segmentation fault I tried everything and any opinions of you. If you can help me and give me some opinions about my code. Thank YOu

    My code is:

    Code:
    #include <stdio.h>
    
    int strlen(char *s)
    {
    char *p = s;
    while (*p != '\0')
    p++;
    return p - s;
    }
    
    char * strsubst(char * s, const char * old_s, const char * new_s)
    {
    char *aux;
    char *aux1;
    int i,j;
    int k=0;
    
    for(i=0; i<strlen(s); i++){
    	if(s[i]==old_s[k])
    		*aux1=k;
    		*aux=i;
    }
    	while(*old_s!='\0'){
    		while(*s !='\0'){
    			if(s[*aux+1]==old_s[*aux1+1])
    				*aux=s[*aux];	
    	}
    		}							
    
    if(aux)
    {
    for(j=0; j<strlen(old_s);j++)
    	s[(aux-s) + j] = new_s[j];
    		return (s);
    }
    else
    return NULL;
    }
    
    int main()
      {
        char string[20],antiga[7],nova[7];
    
        printf("\n");
        printf("Entre com a string :");
        gets(string);
        printf("\n");
        printf("Entre com a antiga palavra :");
        gets(antiga);
        printf("\n");
        printf("Entre com a nova palavra :");
        gets(nova);
        printf("\n");
        printf("nova string ==> %s\n",strsubst(string,antiga,nova));
    
        return(0);
        
      }
    error: Segmentation Fault

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    A general rule of thumb for functions is that (ideally) they should do one thing, and do it well. You kind of got that with your strlen() function (but consider renaming it to my_strlen(), or something else, to avoid conflicts with the standard library version of strlen()). Now what you need to do is implement your own version of strstr(). If you get that, you will decrease the complexity of your code, and your strsubstr() function will be easier to implement. Give it a try.

    Now pay attention to what you have been told about using gets(). Use fgets() instead, or if you have Visual Studio, use gets_s if you wish.
    Last edited by kermit; 09-27-2010 at 07:23 PM.

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Borowsky View Post
    The problem continues to work Segmentation fault I tried everything and any opinions of you. If you can help me and give me some opinions about my code. Thank YOu

    My code is:

    Code:
    #include <stdio.h>
    
    int strlen(char *s)
    {
    char *p = s;
    while (*p != '\0')
    p++;
    return p - s;
    }
    
    char * strsubst(char * s, const char * old_s, const char * new_s)
    {
    char *aux;
    char *aux1;
    int i,j;
    int k=0;
    
    for(i=0; i<strlen(s); i++){
    	if(s[i]==old_s[k])
    		*aux1=k;
    		*aux=i;
    }
    	while(*old_s!='\0'){
    		while(*s !='\0'){
    			if(s[*aux+1]==old_s[*aux1+1])
    				*aux=s[*aux];	
    	}
    		}							
    
    if(aux)
    {
    for(j=0; j<strlen(old_s);j++)
    	s[(aux-s) + j] = new_s[j];
    		return (s);
    }
    else
    return NULL;
    }
    
    int main()
      {
        char string[20],antiga[7],nova[7];
    
        printf("\n");
        printf("Entre com a string :");
        gets(string);
        printf("\n");
        printf("Entre com a antiga palavra :");
        gets(antiga);
        printf("\n");
        printf("Entre com a nova palavra :");
        gets(nova);
        printf("\n");
        printf("nova string ==> %s\n",strsubst(string,antiga,nova));
    
        return(0);
        
      }
    error: Segmentation Fault
    Hi guys.. I have a question about the code above in red; the fact that many people
    have not commented on means I'm already wrong..

    I see aux and aux1 declared as pointers to type char.... but then I see both of
    them being used before they are initialized.

    They must be initialized somewhere, but I don't see where! Thanks for any help.

    Edit: I just scanned the responses again, and in post #6 (claudiu) points this out.
    I thought that since that was a long time ago, the problem was corrected without further comment from anyone else.
    Last edited by Char*Pntr; 09-27-2010 at 08:10 PM. Reason: additional info:

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    No, you are not wrong.

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by kermit View Post
    No, you are not wrong.
    I read your reply too quick.. my heart sank.

    After taking a deep breath, then I noticed the "not" in your reply.

    I hope I'm not blowing it with the revelation... but I saw a lot of
    replies without a comment on that one.

    In the future with something like this, should I just observe?

    Thanks Kermit!

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If you think you see something wrong, muck in and point it out. I think the reason why nobody mentioned it is because there is quite a bit of stuff there that isn't quite right and the code is also, IMO, somewhat confusing. Possibly people missed it altogether (The mistakes were hiding in mistakes) or they could not be bothered typing out every single mistake. I guess it could be kind of disheartening to post your code and have it completely ripped to bits. But feel free to point stuff like that out (If you are pretty sure of it) - If you get it wrong, someone will be sure to tell you.
    Last edited by kermit; 09-27-2010 at 08:13 PM.

  13. #13
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by kermit View Post
    If you think you see something wrong, muck in and point it out. I think the reason why nobody mentioned it is because there is quite a bit of stuff there that isn't quite right and the code is also, IMO, somewhat confusing. If you get it wrong, someone will be sure to tell you.
    Great reply! Thanks..

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I believe my original reply about aux pointing to NULL got the wrong interpretation on the part of the OP. Instead of allocating memory for aux before using it, or making it point to a valid memory location, he just removed the NULL initialization, thinking this would solve the problem.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed