0

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
What is the difference between char a[] = “string”; and char *p = “string”;

I have find the fault ,but I don't know why. so would you help me ?

If I define the char str[] instead the char * str in the main function , it can work normally , else the line of *pSlow = *pFast; will crash with "Unhandled exception at 0x012314f3 in shanchu.exe: 0xC0000005: Access violation writing location 0x0123583c."

Thanks

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

char * delChar(char *s,int iLen)    
{    
    if((s == NULL) || iLen <= 0)    
    {    
        return NULL;    
    }    
    int i;    

    const int MAXLEN = 26;    

    unsigned int min,hashTable[MAXLEN];    

    for(i = 0;i < MAXLEN;i ++)    
    {    
        hashTable[i] = 0;    
    }    

    for(i = 0;i < iLen;i ++)    
    {    
        hashTable[*(s + i) - 'a'] ++;    
    }    

    while(hashTable[i] == 0)    
    {    
        i ++;    
    }    
    min = hashTable[i];    

    for(i = 0;i < MAXLEN;i ++)    
    {    
        if(hashTable[i] != 0)    
        {    
            if(hashTable[i] < min)    
            {    
                min = hashTable[i];    
            }    
        }               
    }    

    char *pSlow = s;  
    char *pFast = s;      
    while(*pFast != '\0')    
    {    
        if(hashTable[*pFast - 'a'] != min)    
        {    
            *pSlow = *pFast;     
            pSlow ++;  
        }           
        pFast ++;  
    }    
    *pSlow = '\0';  

    return s;    
}    
int main()    
{    
    char* str = "abadccdehigiktk";    
    int iLen = strlen(str)/sizeof(char);    
    char *tmp = delChar(str,iLen);    
    printf("%s\n",tmp); 
system("pause");

}    
4

3 回答 3

4
char* str = "abadccdehigiktk";

string-literal不应修改。在您的功能delChar中,您试图修改string-literal. 这是未定义的行为。

你应该使用

char[] str = "abadccdehigiktk"; 

或 mb std::string(因为您使用 C++ 编写)。

于 2012-09-11T05:39:29.010 回答
1

这条线

char* str = "abadccdehigiktk";

定义了一个指向常量字符串的指针,即该字符串不能被修改。如果将其声明为数组 ( char str[]),则它是堆栈上的数组,因此可以修改。

至于字符的删除,为什么不用egmemmove来代替呢?

// "Delete" the fourth character of a string
memmove(str + 3, str + 4, strlen(str) - 3);

如果你改用std::string它,它会突然变得更容易使用std::string::erase

std::string str = "abadccdehigiktk";

// Remove the fourth character
str.erase(3, 1);

您也不必担心指针与数组。

于 2012-09-11T05:41:12.017 回答
0

这部分是完全错误的:

while(hashTable[i] == 0)    
{    
    i ++;    
}    
min = hashTable[i];    

for(i = 0;i < MAXLEN;i ++)    
{    
    if(hashTable[i] != 0)    
    {    
        if(hashTable[i] < min)    
        {    
            min = hashTable[i];    
        }    
    }               
}    

首先i尚未初始化,因此它将超出范围(由于之前的“for”循环,它最初将等于“iLen”)。其次,逻辑是一团糟,你可以在一个循环中做到这一点。我认为您可能在这里尝试做的是:

min = UINT_MAX;
for (i = 0; i < MAXLEN; i++)    
{
    if (hashTable[i] > 0 && hashTable[i] < min)
    {
        min = hashTable[i];
    }
}

即在哈希表中找到最小的非零值。

于 2012-09-11T05:40:28.340 回答