2013-02-20 115 views
0

我的代码如下:strcat的和段故障11

#include<stdio.h> 
    #include <string.h> 
    #include <math.h> 
    #include <stdlib.h> 
    int string_length(char*); 
    void reverse(char*); 
    int main(void) 
    { 
     char num[256]; 
     printf("%c[%d;%d;%dmPlease enter a number of rows: ", 0x1B, 5, 32, 40); 
     gets(num); 
     //gets number 

printf("%c[%dm\n", 0x1B, 0); 
//Resets color 

//Variables 
char revnum[50], q[50] = "\0", r[50] = "\v"; 
int i, x, j, z, w; 

//Reverses inputted character string 
reverse(num); 

//Takes reversed inputted character string and out puts it as an integer 
for(i = 0; num[i] != '\0'; ++i) { 
    j = -(48-num[i]); 
    double y = i; 
    x = j*pow(10, y) + x; 
} 

//Takes integer version of inputted character string and assigns a counter's value (counting up to our integer version) to a character string 
for (z = 0; z <= x; z++) { 
    sprintf(revnum, "%d", z); 

    //Takes the new character string for each number up to the inputted value and prints it vertically, but no differentiating between numbers and printing them horizontally to each other 
    for (w = 0; revnum[w] != '\0'; ++w) { 
     strcat(q, r); 
     printf("%s", q); 
     strcat(q, revnum[w]); 
     } 
    } 

    } 

    //Function which reverses a character string 
    void reverse(char *num) 
{ 
int length, c; 
char *begin, *end, temp; 

length = string_length(num); 

begin = num; 
end = num; 

for (c = 0 ; c < (length - 1) ; c++) 
    end++; 

for (c = 0 ; c < length/2 ; c++) 
{ 
    temp = *end; 
    *end = *begin; 
    *begin = temp; 

    begin++; 
    end--; 
} 
} 

int string_length(char *pointer) 
{ 
int c = 0; 

while(*(pointer+c) != '\0') 
    c++; 

return c; 
} 

方案的一点是要输出前所有的号码与每个号码的垂直的数字,然后数字本身横向排列所输入的数。

请帮忙!!!

+2

尝试在调试器中运行您的程序。它会告诉你崩溃的位置,让你走过函数调用堆栈,这样你就可以看到你如何在那里结束,并让你检查变量来帮助你找出原因。 – 2013-02-20 02:28:04

+0

虽然有几件事情:当'strlen'完全正常时,为什么你要自己创建字符串长度函数?而不是在'reverse'中循环查找字符串的末尾,你可以在指针上加'(length - 1)'。 – 2013-02-20 02:30:11

+0

但是,主要的问题可能是你并没有初始化你使用的所有变量。如果你使用例如在编译你的时候给GCC选项'-Wall'会得到一个警告。 – 2013-02-20 02:32:06

回答

0

你犯了几个错误。此代码的工作:

#include<stdio.h> 
#include <string.h> 
#include <math.h> 
#include <stdlib.h> 
int string_length(char*); 
void reverse(char*); 
int main(void) 
{ 
    char num[256]; 
    printf("Please enter a number of rows: ", 0x1B, 5, 32, 40); 
    gets(num); 
    //gets number 

//Variables 
char revnum[50], q[50] = "\0"; 
int i, x, j, z, w; 

//Reverses inputted character string 
reverse(num); 

//Takes reversed inputted character string and out puts it as an integer 
x = atoi(num); 

//Takes integer version of inputted character string and assigns a counter's value (counting up to our integer version) to a character string 
for (z = 0; z <= x; z++) { 
sprintf(revnum, "%d", z); 

//Takes the new character string for each number up to the inputted value and prints it vertically, but no differentiating between numbers and printing them horizontally to each other 
for (w = 0; revnum[w] != '\0'; ++w) { 
    printf("%s\v", q); 
char a[2]; 
sprintf(a,"%c",revnum[w]); 
strcat(q,a); 
    } 
} 

} 

//Function which reverses a character string 
void reverse(char *num) 
{ 
int length, c; 
char *begin, *end, temp; 

length = string_length(num); 

begin = num; 
end = num; 

for (c = 0 ; c < (length - 1) ; c++) 
end++; 

for (c = 0 ; c < length/2 ; c++) 
{ 
temp = *end; 
*end = *begin; 
*begin = temp; 

begin++; 
end--; 
} 
} 

int string_length(char *pointer) 
{ 
int c = 0; 

while(*(pointer+c) != '\0') 
c++; 

return c; 
} 

配备选项-fno-堆栈保护编译,以防止堆栈smaching检测。