Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
strcat和分段故障11_C_String_Printf_Strcat - Fatal编程技术网

strcat和分段故障11

strcat和分段故障11,c,string,printf,strcat,C,String,Printf,Strcat,我的代码如下: #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

我的代码如下:

   #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;
}
#包括
#包括
#包括
#包括
整数字符串长度(字符*);
无效反向(字符*);
内部主(空)
{
字符数[256];
printf(“%c[%d;%d;%d请输入若干行:”,0x1B,5,32,40);
获取(num);
//获取数字
printf(“%c[%dm\n”,0x1B,0);
//重置颜色
//变数
char revnum[50],q[50]=“\0”,r[50]=“\v”;
int i,x,j,z,w;
//反转输入的字符串
反向(num);
//获取反向输入的字符串并将其作为整数输出
对于(i=0;num[i]!='\0';++i){
j=-(48个数值[i]);
双y=i;
x=j*pow(10,y)+x;
}
//获取输入字符串的整数版本,并将计数器的值(计数到整数版本)分配给字符串

对于(z=0;z您犯了几个错误。此代码有效:

#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;
}
#包括
#包括
#包括
#包括
整数字符串长度(字符*);
无效反向(字符*);
内部主(空)
{
字符数[256];
printf(“请输入若干行:”,0x1B,5,32,40);
获取(num);
//获取数字
//变数
char revnum[50],q[50]=“0”;
int i,x,j,z,w;
//反转输入的字符串
反向(num);
//获取反向输入的字符串并将其作为整数输出
x=原子数(num);
//获取输入字符串的整数版本,并将计数器的值(计数到整数版本)分配给字符串

对于(z=0;z尝试在调试器中运行您的程序。它会告诉您崩溃的位置,让您了解函数调用堆栈,以便了解最终结果,并让您检查变量以帮助您找出原因。不过有两件事:当
strlen 非常好吗?您可以只添加
(长度-1),而不是在
反向中循环查找字符串的结尾
指向指针。但是,主要问题可能是您没有初始化所有使用的变量。如果您在编译时将选项
-Wall
用于GCC,则会收到有关此的警告。您应该从编译器中至少收到一个编译器警告。如果是,请修复代码,直到它编译干净。如果您没有,了解如何提高编译器的警告级别。
strcat(q,revnum[w]);
char
作为第二个参数传递给
strcat
;它需要
char*
。并且永远不要使用
get()
函数;它本质上是不安全的。
字符串长度
?标准C
strlen()
在过去30年中我们都不知道有什么问题吗?:-)