程序已停止在C中工作

程序已停止在C中工作,c,function,C,Function,我有一个项目,其中包括创建一个复杂的计算器。我已经完成了,但是我没有使用函数(直到完成一半我才知道它们)。我想只使用函数重新制作项目,main()上唯一的功能就是链接到外部函数。我在乞讨时遇到了一个问题,程序一直没有响应。我的目标是从类型化的字符串中删除所有空白空间,所以在我的SCANF上使用了 %[^ \n\%*c ] < /代码>。我不知道为什么它不起作用,我需要一些帮助。谢谢 #include <stdio.h> char espaco(char equacao[]){

我有一个项目,其中包括创建一个复杂的计算器。我已经完成了,但是我没有使用函数(直到完成一半我才知道它们)。我想只使用函数重新制作项目,main()上唯一的功能就是链接到外部函数。我在乞讨时遇到了一个问题,程序一直没有响应。我的目标是从类型化的字符串中删除所有空白空间,所以在我的SCANF上使用了<代码> %[^ \n\%*c ] < /代码>。我不知道为什么它不起作用,我需要一些帮助。谢谢

#include <stdio.h>
char espaco(char equacao[]){
    int i=0,j=0;
    char x[40];
    for(i=0;i<40;i++){
        if(equacao[i]!= ' '){
            x[j]=equacao[i];
            j++;
        }
    }
    return x[40];
}
int main()
{
    char equacao[50];
    char x[50];
    scanf("%[^\n]%*c",&equacao[50]);
    x[40]=(espaco(equacao[50]));
    printf("%s",x[40]);
    return 0;
}
#包括
char espaco(char equaco[]{
int i=0,j=0;
charx[40];

对于(i=0;i您的代码包含多个错误。如果您给一个好的编译器一个机会并打开警告,大多数错误都会被它捕获。下面是一个可能的解决方案的注释草图

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *espaco(char equacao[])
{
  int i = 0, j = 0;
  // you returned local memory in the original version,
  // just as the compiler would have told you had you switched the warnings on
  for (i = 0; i < 50; i++) {
    // does not skip tabs, is that intentional?
    if (equacao[i] != ' ') {
      // we strip characters, that means we can use the
      // orignal array as the destination, too
      equacao[j] = equacao[i];
      j++;
    }
  }
  // return a pointer to the result for convenience
  return equacao;
}

int main()
{
  int res;
  // reserve array of 50 char on the stack
  char equacao[50];
  // reserve another array of 50 char on the stack
  char x[50];
  // you have only 50 places to put a character in, plus the NUL, so
  // limit it to 49.
  res = scanf("%49[^\n]%*c", equacao);
  // scanf() might have failed, check.
  if (res != 1) {
    fprintf(stderr, "Something went wrong with scanf\n");
    exit(EXIT_FAILURE);
  }
  // you need to copy it explicitely, assigning it is not enough
  // Just like the compiler would have told you, if you had you switched the warnings on
  memcpy(x, espaco(equacao), 50);
  // espaco() works in-place, you can check it here
  // printf("%s\n", equacao);
  // if unsure, finish the string with a NUL manually,
  // but scanf() should have taken care of it
  // x[49] = '\0';
  printf("%s", x);
  exit(EXIT_SUCCESS);
}
#包括
#包括
#包括
char*espaco(char equaco[]
{
int i=0,j=0;
//您在原始版本中返回了本地内存,
//就像编译器告诉你的那样,如果你打开了警告的话
对于(i=0;i<50;i++){
//不跳过标签页,这是故意的吗?
如果(equacao[i]!=''){
//我们剥离字符,这意味着我们可以使用
//也将原始数组作为目标
equacao[j]=equacao[i];
j++;
}
}
//为方便起见,返回指向结果的指针
返回equacao;
}
int main()
{
国际关系;
//在堆栈上保留50个字符的数组
char equacao[50];
//在堆栈上保留另一个50个字符的数组
charx[50];
//你只有50个位置可以放置一个角色,加上NUL,所以
//限制在49。
res=scanf(“%49[^\n]%*c”,equalaco);
//scanf()可能已失败,请检查。
如果(res!=1){
fprintf(stderr,“scanf出了问题”\n);
退出(退出失败);
}
//您需要明确地复制它,分配它是不够的
//就像编译器会告诉你的那样,如果你打开了警告的话
memcpy(x,espaco(Equaco),50岁);
//espaco()在适当的位置工作,您可以在此处进行检查
//printf(“%s\n”,equaco);
//如果不确定,请手动使用NUL完成字符串,
//但是scanf()应该处理好它
//x[49]='\0';
printf(“%s”,x);
退出(退出成功);
}

您的代码存在很多问题。我将在下面尝试解决这些问题

您的代码副本:

#include <stdio.h>
char espaco(char equacao[]){   // Here you tell the function to return a char,
                               // i.e. just a single character like an 'a' or '8'.
                               // It that really what you want? Or did you
                               // want to return an array?
    int i=0,j=0;
    char x[40];
    for(i=0;i<40;i++){         // Here you loop to 39 but there might not be 39
                               // characters in equacao so this is illegal code in 
                               // many cases as equacao[i] may be uninitialized
                               // Use: for(i=0;i<strlen(equacao);i++){
        if(equacao[i]!= ' '){
            x[j]=equacao[i];
            j++;
        }
    }
    return x[40];              // Here you index the array with 40. That is not allowed
                               // The valid indexes are from 0 to 39
}


int main()
{
    char equacao[50];
    char x[50];
    scanf("%[^\n]%*c",&equacao[50]);  // Here are two issues
                                      // 1) &equacao[50] shall be just equacao
                                      //    or &equacao[0]
                                      //
                                      // 2) You overflow the buffer if the user types
                                      //    more than 50 characters.
                                      //    Use fgets instead of scanf

    x[40]=(espaco(equacao[50]));      // Assignment to x[40] is illegal.
                                      // The valid indexes are from 0 to 39

    printf("%s",x[40]);               // Here %s means: print a string
                                      // but you try to give it a char.
                                      // However, x[40] is illegal index
    return 0;
}

必须使用指针,记住C中的字符串是字符数组

#include <stdio.h>
 char *espaco(char *equacao){
    int i=0,j=0;
    static char x[40];
    for(i=0;i<40;i++){
        if(equacao[i]!= ' '){
            x[j]=equacao[i];
            j++;
        }
    }
    return x;
}
int main()
{
    char *equacao[40];
    char *x[40];
    scanf("%[^\n]%*c", &equacao[40]);
    x[40]=(espaco(equacao[40]));
    printf("%s",x[40]);
    return 0;
}
#包括
char*espaco(char*equacao){
int i=0,j=0;
静态字符x[40];

对于(i=0;i您的代码似乎有很多机会超出数组边界。第一步是启用编译器警告并修复它们。例如,
scanf
的参数应该是
equaco
@JohnBollinger,我想是这样。我们不能在这个项目中使用动态数组,所以我必须设置一个限制。谢谢。@WeatherVane,所以我在扫描数组时没有设置[40]?我会重新编写代码,谢谢。“我不知道如何编写满足我约束的好代码”是编写错误代码的一个可以理解的原因,但很少是一个可以接受的原因。您是否可以使用动态分配并不能真正考虑您是否可以确保不会超出数组的界限。请修复(i=0;i<50;i++)
@4386427我看不到您描述的错误,请详细说明。1)
equaco[i]
如果输入较短,则可能正在访问未初始化的字符49。要么初始化整个数组,要么最好使用
strlen
来确定何时停止循环3)使用
memcpy
不是一个错误,但使用
strcpy
似乎更清楚。毕竟,代码处理的是字符串。4)如果用户只按newline/enter会发生什么情况?试试看。
scanf
通常不好。改用
fgets
。我不认为这是OP想要的,但至少要修复:
x[40]=…
for(I=0;这真的很有帮助,我会尝试改进我的代码并给出一些反馈,非常感谢您的时间。
#include <stdio.h>
 char *espaco(char *equacao){
    int i=0,j=0;
    static char x[40];
    for(i=0;i<40;i++){
        if(equacao[i]!= ' '){
            x[j]=equacao[i];
            j++;
        }
    }
    return x;
}
int main()
{
    char *equacao[40];
    char *x[40];
    scanf("%[^\n]%*c", &equacao[40]);
    x[40]=(espaco(equacao[40]));
    printf("%s",x[40]);
    return 0;
}