Search code examples
cgccruntime-errorbusmemmove

Bus error when using memmove


Possible Duplicate:
Bus error troubleshooting

To remove duplicates from a string this is the program I have written:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void remDup(char str[])
{
    int i=0,len;
int arr[256]={[0 ... 255] = 0};

while(str[i]!='\0')
{   
    len=strlen(str);
    if(arr[str[i]]==1)
    {
        memmove(str+i,str+i+1,len);
    }
    else
        arr[str[i]]=1;
    i++;
}

printf("String with Unique Characters:%s\n",str);

}

main()
{
remDup("kjijhgfedcaba");
}

But the error displayed on running the program is: Bus error: 10

What changes have to made in the code? Thanks in advance


Solution

  • "kjijhgfedcaba" is a string literal and you cannot modify a string literal in C.

    By the way if you want to initialize all the arr elements to 0, instead of this (which is a GNU extension):

    int arr[256]={[0 ... 255] = 0};
    

    you can simply do this:

    int arr[256]= {0};