-6

私はCが初めてで、すべてのメモリとポインターを理解するのに問題があるようです。だから私は次のJavaコードを持っています:

public class Question_10
{
   public static void sortInt(int[] array)
   {
      int top = 0;
      while (top < array.length - 1)
      {
         if (array[top] < array[top + 1])
         {
            top++;
         }
         else
         {
            int temp = array[top];
            array[top] = array[top + 1];
            array[top + 1] = temp;
            if (top > 0)
            {
               top--;
            }
         }
         System.out.println(top);
      }
   }

   public static void main(String[] args)
   {
      int[] arr = {5, 6, 10, 1, 45, 3};

      sortInt(arr);
      System.out.println();
   }

}

ああ、私は次のことをしました:

#include <stdio.h>

void sortInt(int arrayInput[])
{
    int top = 0;
    int arrLen = sizeof(arrayInput)/(sizeof(int);
    while(top < arrLen - 1)
    {
        if(arrayInput[top] < arrayInput[top+1])
        {
            top++;
        }
        else
        {
            int temp = arrayInput[top];
            arrayInput[top] = arrayInput[top + 1];
            arrayInput[top + 1] = temp;
            if(top > 0)
            {
                top--;
            }
        }
        printf("%i", top);
    }
}
void main()
{

    int array[] = {5, 6, 10, 1, 45, 3};
    sortInt(array);
    return 0;
}

もちろん、私は多くのエラーを受け取ります:

$ gcc Question10.c
Question10.c: In function `sortInt':
Question10.c:6: error: parse error before ';' token
Question10.c: At top level:
Question10.c:16: error: `top' undeclared here (not in a function)
Question10.c:16: warning: data definition has no type or storage class
Question10.c:17: error: `temp' undeclared here (not in a function)
Question10.c:17: warning: data definition has no type or storage class
Question10.c:18: error: parse error before "if"
Question10.c:23: error: parse error before string constant
Question10.c:23: error: conflicting types for 'printf'
Question10.c:23: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
Question10.c:23: error: conflicting types for 'printf'
Question10.c:23: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
Question10.c:23: warning: data definition has no type or storage class
Question10.c: In function `main':
Question10.c:30: warning: `return' with a value, in function returning void
Question10.c:27: warning: return type of 'main' is not `int'
Question10.c:31:2: warning: no newline at end of file
Question10.c: At top level:
Question10.c:16: error: storage size of `arrayInput' isn't known
Question10.c:17: error: storage size of `arrayInput' isn't known

C のこれらの「オブジェクト」のことで本当に迷子になっているので、何が問題なのかについて何か提案をしてください。一般的なガイダンスが役立つでしょう。

4

2 に答える 2

1

次の行に構文エラーがあります。

int arrLen = sizeof(arrayInput)/(sizeof(int);

代わりに次を試してください。 int arrLen = sizeof(arrayInput)/sizeof(int);

それを修正することから始めれば、あなたが抱えている他の問題のいくつかを解決できるかもしれません. そうでない場合は、一度に 1 つずつ服用してください。

私が見ることができる別のエラーは、メイン メソッドを として宣言しvoid main()、 を呼び出すことreturn 0;です。C標準では main() メソッドは を返すはずなintので、宣言を変更します。

于 2012-12-18T01:20:51.137 に答える
1

私はそれを動作させました。

   void sortInt(int arrayInput[], int arrLen)
    {
        int top = 0;
        while(top < arrLen - 1)
        {
            if(arrayInput[top] < arrayInput[top+1])
            {
                top++;
            }
            else
            {
                int temp = arrayInput[top];
                arrayInput[top] = arrayInput[top + 1];
                arrayInput[top + 1] = temp;
                if(top > 0)
                {
                    top--;
                }
            }
            printf("%i", top);
        }
    }

    int main()
    {
        int array[] = {5, 6, 10, 1, 45, 3};
        sortInt(array, sizeof(array)/sizeof(int));
        return 0;
    }
于 2012-12-18T01:28:21.880 に答える