Question

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

Was it helpful?

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

OTHER TIPS

Try len-i for the size of memory being moved. You definitely move memory that doesn't belong to you. (that said, I haven't really read the code).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top