2013-02-20 73 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檢測。