gpt4 book ai didi

c - strcat 和段错误 11

转载 作者:行者123 更新时间:2023-11-30 17:54:23 25 4
gpt4 key购买 nike

我的代码如下:

   #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;
}

程序的要点是输出输入的数字之前的所有数字,每个数字的数字垂直,然后数字本身水平列出。

请帮忙!!!

最佳答案

你犯了几个错误。此代码有效:

#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-stack-protector进行编译以防止堆栈恶意检测。

关于c - strcat 和段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com