Thread: String segmentation fault

  1. #1
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23

    String segmentation fault

    I have written code to expand a string like "a2b3" in "aabbb" in same string.

    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char str[50],var;
        int i=0,sh=0,len;
        printf("Enter String to expand: ");
        scanf("%s",str);
        len=strlen(str);
        while(str[i] != '\0')
        {
            var=str[i];
            printf("var is:%c ",var);
            sh=shift(&str[i]);
            printf("shift no is:: %d and i is: %d ",sh,i);
            while(i<i+sh)
            {
                str[i]=var;
                i++;
            }
            //break;
        }
        printf("%s",str);
        return 0;
    }
    int shift(char *st)
    {
        char ch=*(st+1);
        char *temp=st;
        int shiftno=ch-48,i,len;
        printf("passed string is: %s  no is %d \n",st,shiftno);
        if(shiftno <= 2)
        {
            while(*(st+2-shiftno) != '\0')
            {
                *st=*(st+2-shiftno);
                st++;
            }
            *st='\0';
            printf("fun  :%s",temp);
        }
        else{
            len=strlen(st);
            for(i=len;i>0;i--)
                *(st+i+shiftno-2)=*(st+i);
        }
        return (shiftno);
    }
    The above program is showing segmentation error after taking input and getting hang somewhere in shift() function.But when i make comment while loop in main() then shift function is working fine.
    Is anyone can explain me this problem?
    plz...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ooooh!


    You should be using either expandable arrays, or malloc/realloc for this. Interesting concept, but not a good one to use, imo.
    Last edited by Adak; 08-29-2012 at 12:38 AM.

  3. #3
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    I don' t think because of that i m getting segmentation fault...
    In main function strlen() returned value is not used and in shift() i think it will work....

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your problem is with this
    Code:
    while(i<i+sh)
    Think about it. Goodnight.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, I read that wrong. It's getting late. I'm off to bed.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Code:
    while(i<i+sh)
            {
                str[i]=var;
                i++;
            }
    What are you trying to do here?
    This loop going to be infinite as 'i' is always lessthan 'i+sh'.
    When it crosses the length of str[] it will give you segmentation fault.

  7. #7
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Thanks, That was my mistake now it's working fine but one more query is that when i was not writting while loop(commenting) control comes till above the while loop(printing line no 15) but after writing while loop the printf statement just written above while loop (line no 15) was not printing but it should print and then show segmentation fault..

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dojha00 View Post
    ... but after writing while loop the printf statement just written above while loop (line no 15) was not printing but it should print and then show segmentation fault..
    You cannot be shure about that. stdout is buffered.
    If stdout is linebuffered add '\n' to the format string or better even add fflush(stdout); before the loop.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too long string causes segmentation fault
    By heinz55 in forum C Programming
    Replies: 11
    Last Post: 08-07-2012, 06:07 PM
  2. Pointing at a string gave me segmentation fault
    By hzcodec in forum C Programming
    Replies: 5
    Last Post: 06-14-2012, 03:58 PM
  3. Segmentation fault when changing a string
    By lilydjwg in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:43 AM
  4. segmentation fault when processing a string
    By Nakel in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:02 PM
  5. segmentation fault when processing a string
    By EMC2 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2003, 02:56 PM