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;
}
프로그램의 포인트는 모든 숫자 자체가 수평으로 나열 수직 각 번호의 숫자와 그 숫자 입력 된 번호 앞에 출력이다 .
도와주세요!
프로그램을 디버거에서 실행 해보십시오. 그것은 충돌이 어디 있는지 알려주고, 함수 호출 스택을 걸어서 거기에서 어떻게 끝났는지 알 수있게하고, 변수를 검사하여 이유를 파악하는 데 도움을줍니다. –
두 가지가 있습니다 :'strlen'이 완벽 할 때, 왜 당신 자신의 문자열 길이 함수를 만드나요? 그리고'reverse '에서 문자열의 끝을 찾기 위해 반복하는 대신'(length-1)'을 포인터에 추가 할 수 있습니다. –
그러나 주된 문제는 사용하는 모든 변수를 초기화하지 않는다는 것입니다. 예를 들어 컴파일 할 때 GCC에'-Wall' 옵션을 쓰면 경고 메시지가 나옵니다. –