【问题标题】:translate Java code to C [closed]将Java代码翻译成C [关闭]
【发布时间】:2012-12-18 01:19:09
【问题描述】:

我是 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 中的这些“对象”事物中。

【问题讨论】:

  • 然后停止使用它们。端口算法,而不是代码。
  • 错误很明显,第6行更多。
  • 重要提示:int arrLen = sizeof(arrayInput)/sizeof(int); 不适用于sortInt()。函数接收的参数是指向array 的第一个元素的指针,因此无论数组的长度如何,都将arrLen 设置为sizeof(int*) / sizeof(int)。您必须将长度作为参数传递。

标签: java c pointers translation


【解决方案1】:

这一行有语法错误:

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

尝试改用: int arrLen = sizeof(arrayInput)/sizeof(int);

如果您从修复它开始,那么您可能会解决您遇到的其他一些问题。如果没有,一次拿一个。

我可以看到的另一个错误是您将 main 方法声明为 void main() 并且您调用了 return 0;。按照C标准,main()方法应该返回int,所以修改声明吧。

【讨论】:

  • "尝试改为" --- 不,不要;这会产生错误的值,因为 arrayInput 是一个指针(由于衰减),而不是一个数组。
  • @JimBalter 好点。我更关注语法错误,而不是预期的结果。
【解决方案2】:

我做到了。

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

【讨论】:

  • @YogendraSingh 因为函数接收的参数是指向数组第一个元素的指针。无法确定函数内部的数组大小。
  • @YogendraSingh 不,它当然不能(C 编程 .101)——arrayInput 是一个指针,而不是一个数组。
猜你喜欢
  • 1970-01-01
  • 2016-08-04
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
相关资源
最近更新 更多