2017-10-10 61 views
1

所以它会打印:“AAA QQQ CCC ddd bbb“

但由于某些原因,我不理解它会抛出异常。 我在代码中写了错误。 我知道我的代码有点混乱,所以如果你也有建议,我很乐意听到。

谢谢!!!

void changeWords(char *s,int X, int Y) 
{ 
    int len,words,i,count; 
    len = count = words = i = 0; 
    bool flag = false; 
    while (s[len] != ' ') 
     len++; 
    char *p1 = (char*)malloc(sizeof(char)*(len+1)); 
if (p1== NULL) 
{ 
    printf("Error: memory did not allocated"); 
    exit(1); 
} 
    char *p2 = (char*)malloc(sizeof(char)*(len+1)); 
if (p2== NULL) 
{ 
    printf("Error: memory did not allocated"); 
    exit(1); 
} 
    while (flag == false) 
    { 
     if (count == (X-1)) 
     { 
      for(int x = 0; x< len;x++,i++) 
       p1[x] = s[i]; 
     } 
     else if (count == (Y-1)) 
     { 
      for (int x = 0; x< len; x++,i++) 
       p2[x] = s[i]; 
      flag = true; 
     } 
     if (s[i] == ' ') 
      count++; 
     i++; 
    } 
    p1[len] = p2[len] = '\0'; 
    i = count = 0; 
    flag = false; 
    while (flag == false) 
    { 
     if (count == (X-1)) 
     { 
      for (int x = 0; x< len; x++, i++) 
       s[i] = p1[x]; // here it throw an error "Unhandled exception thrown:.." 
     } 
     else if (count == (Y-1)) 
     { 
      for (int x = 0; x< len; x++, i++) 
       s[i] = p2[x]; 
      flag = true; 
     } 
     if (s[i] == ' ') 
      count++; 
     i++; 
    } 
    puts(s); 
    free(p1); free(p2); 
} 
void main() 
{ 
char*str = (char*)malloc(sizeof(char)); 
if (str == NULL) 
{ 
    printf("Error: memory did not allocated"); 
    exit(1); 
} 
char ch; 
int i = 0; 
printf("Enter a string: "); 
while ((ch = getchar()) != '\n') 
{ 
    str[i] = ch; 
    i++; 
    str = realloc(str, sizeof(char) * (i + 1)); 
    if (str == NULL) 
    { 
     printf("Error: memory did not allocated"); 
     exit(1); 
    } 
} 
str[i] = '\0'; 
func(str,3,5); 
printf("new string: %s\n", str); 

free(str); 
system("pause"); 

} 
} 
+1

你是否在调试器中检查了代码并检查了标记的行上的值? – xxbbcc

+0

字符串文字不能更改。 – BLUEPIXY

+0

's'是一个字符串文字,并且正在'changeWords'中修改它,这是未定义的行为。改为'char str [] =“aaa bbb ccc ddd qqq”;'(但不保证这是唯一的问题) – yano

回答

2

当你这样做:

char * str = "aaa bbb ccc ddd qqq"; 

字符串字面"aaa bbb ccc ddd qqq"被放置在只读数据段,并str由指向它。在这一点上,改变str指向的是未定义的行为,并希望能够引发段错误。

如果你绝对要使用指针,试试这个来代替:

char * str = strdup("aaa bbb ccc ddd qqq"); 

这将使字符串的副本堆上,这将是可写的文字。

一旦你完成它,但你需要free(str);释放内存。

+0

谢谢!你是对的,当我做到了,它是一个常量,所以它不起作用 – asaf

+0

@asaf注意,'strdup'不是标准C函数。 – gsamaras

+0

'strdup'在POSIX和[动态内存TR](http://en.cppreference.com/w/c/experimental/dynamic)中指定,Windows也有一个,所以代表性很广。 – rustyx

1

更改此:

char * str = "aaa bbb ccc ddd qqq"; 

这样:

char str[] = "aaa bbb ccc ddd qqq"; 

,因为第一个是字符串文字

字符串常量不能被修改,因此您的代码调用未定义行为,在你的函数,其中s实际上是字符串文字:

s[i] = p1[x]; 

PS:无关您的问题:What should main() return in C and C++?int


编辑:

如果你真的想使用指针,那么你可以创建数组,并点的指针的第一个元素。或者,你可以动态地为你的字符串分配内存,这将是一个矫枉过正的问题。

+0

我用过:char * str =“aaa bbb ccc ddd qqq”;只是为了检查代码,当我完成你将输入你想要的文本而不知道长度,所以我使用指针 – asaf

+0

我看@asaf,但你不能用字符串文字来做,因为它们不能被修改。 – gsamaras

0
  • 的变量temp不使用
  • 长度计算是错误的。使用来自文库string.h中的strlen:int len = strlen(s)
  • 我也改变了主要功能:

    INT主(){ 炭STR [20] = “AAA BBB CCC DDD QQQ”;

    changeWords(str, 3, 5); 
    printf("new string: %s\n", str); 
    return 0; 
    

    }

  • 当我执行,它没有任何异常。但是你有逻辑问题。你必须找到用''来打破字符串的单词。你也可以使用strtok函数。

+0

我不想使用数组我想使用指针和学习,并在那得到更好的,也是我想在主要输入自由文本,我会编辑程序 – asaf