程序已停止在C中工作

时间:2016-10-04 18:54:22

标签: c function

我有一个项目,包括创建一个复杂的计算器。我已经完成了但是我没有使用它们(我对它们一无所知,直到中途完成)。我想仅使用函数重新制作项目,而我的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;
}

3 个答案:

答案 0 :(得分:0)

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

代码的副本:

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

如果您确实要更改x中的main,则应将其传递给以下函数:

void espaco(char equacao[], char x[]){    // MAKE THE FUNCTION VOID
    int i=0,j=0;
    // char x[40];  REMOVE THIS LINE
    ....
    ....
    // return x[40];   REMOVE THIS LINE

}

答案 1 :(得分:-1)

您的代码包含多个错误。如果你给它一个机会并切换警告,大多数都被一个好的编译器捕获。这是一个可能的解决方案的评论粗略草图。

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

答案 2 :(得分:-1)

你必须使用指针,记住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;
}