2015-09-05 124 views
1

我刚刚创建了一个程序,它会在缓冲区中找到一个命令。问题是我在代码开始时立即收到分段错误。我甚至在main的开头尝试了打印语句,但它仍然没有显示任何内容。分割错误

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

void convertToUpperCase(char str[], int _size) 
{ 
    int i = 0; 
    while(i < _size) 
    { 
     str[i] = toupper(str[i]); 
     i++; 
    } 
} 

int CheckText(char command[], 
       char buffer[], 
       int starting_point, 
       int size_of_buffer, 
       int size_of_command) 
{ 
    printf("%s", buffer); 
    size_of_command--; 
    convertToUpperCase(buffer, size_of_buffer); 
    convertToUpperCase(command, size_of_command); 

    int i = 0; 
    int r = 0; 
    while(i < size_of_command) 
    { 
     if(buffer[i+starting_point] == command[i]) 
     { 
     r++; 
     printf("%d", r); 
     } 
     i++; 
    } 

    int flag = 0; 
    if(r == size_of_command) 
    { 
     flag = 1; 
    } 

    return flag; 
} 

int main() 
{ 
    char h[40] = "hello"; 
    int fla; 
    printf("hey1"); 
    fla=CheckText("hello", h,0,40,6); 
    if(fla == 1) 
    { 
     printf("command recognized "); 
    } 
    //printf("%s\n", h); 

    return 0; 
} 
+0

调试器说什么?也请重新格式化并重新加载代码以使其更易于阅读。 –

+3

'CheckText(“hello”,h,0,40,6);'注意不能更改的字符串字面值。 – BLUEPIXY

回答

0

正如通过@BLUEPIXY,使用

CheckText("hello", h,0,40,6); 

CheckText修改自"hello"放置在码的只读部分它的参数导致未定义的行为指出。使用类似于:

char h[40] = "hello"; 
char h2[40] = "hello"; 
int fla; 
printf("hey1"); 
fla=CheckText(h2, h,0,40,6);