将Java代码翻译成C

将Java代码翻译成C,java,c,pointers,translation,Java,C,Pointers,Translation,我是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]) {

我是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();
   }

}

也许你可以给我一些关于错误的建议,一些一般性的指导会很有帮助,因为我真的迷失在C中的这些“对象”的东西中。

这行有一个语法错误:

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

请尝试以下方法:
int arrLen=sizeof(arrayInput)/sizeof(int);

如果你开始解决这个问题,那么你可能会解决一些其他的问题。如果没有,一次解决一个

我可以看到的另一个错误是,您将main方法声明为
void main()
,并调用
return 0;
。根据C标准,main()方法应该返回
int
,因此更改声明。

我让它工作了

   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;
    }
void排序(int arrayInput[],int arrLen)
{
int-top=0;
而(顶部0)
{
顶部--;
}
}
printf(“%i”,顶部);
}
}
int main()
{
int数组[]={5,6,10,1,45,3};
sortInt(数组,sizeof(数组)/sizeof(int));
返回0;
}

然后停止使用它们。端口算法,而不是代码。错误很明显,更多见第6行。重要说明:
int arrLen=sizeof(arrayInput)/sizeof(int);
sortInt()
中不起作用。函数接收的参数是指向
数组的第一个元素的指针,因此您可以将
arrLen
设置为
sizeof(int*)/sizeof(int)
与数组长度无关。必须将长度作为参数传递。@YogendraSingh,因为函数作为参数接收的是指向数组第一个元素的指针。无法确定函数内部的数组大小。@YogendraSingh不,当然不能(C编程.101)--arrayInput是指针,而不是数组。“请尝试使用“---不,不要;这会产生错误的值,因为arrayInput是指针(由于衰减),而不是数组。@JimBalter说得很好。我更关注语法错误,而不是预期的结果。
$ 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
   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;
    }