【问题标题】:I need help. First time writing C我需要帮助。第一次写C
【发布时间】:2021-07-04 18:30:56
【问题描述】:

有人可以帮我修复这段代码中的语法错误吗?

#include <stdio.h>
int result(int v, int size);

int main(void){
    int arr[5], n;
    for (n = -1; n < 4; n++){
        arr[n] = n + 1;
        printf("the product of entered values is %d", result(n, 5));
    }
    return 0;
}

int product(int a[]) {
    int product, i;
    for (i = 0; i <= sizeof(int); i++){
        product *= a[i];
    }
    return product;
}

【问题讨论】:

  • 如果您遇到错误,请在帖子中显示。您是否搜索过这些错误?通常,只需将整个错误粘贴到搜索引擎中,就会显示有关如何修复它的相关信息的帖子。最后,请使用一致的空格和缩进格式化您的代码以使其可读。
  • 编译因以下错误而失败。/tmp/ccKrYzlA.o: In function main': /home/main.c:20: undefined reference to result' collect2: error: ld returned 1 exit status
  • 数组从 0 开始,而不是 -1。
  • 那么result在哪里?
  • 错误不是自我解释的吗?您正在调用result,但没有提供该函数的任何定义。它是否在您未显示的其他地方定义?

标签: arrays c for-loop indexing function-definition


【解决方案1】:
for (n = -1; n < 4; n++){
    arr[n] = n + 1;

您从-1 开始n,然后立即使用arr[n]

C 中不允许使用负索引。有效值为 0size of the array-1
换句话说,如果您有 arr[5],则有效索引为 [0][1][2][3][4]


int product(int a[]) {
    int product, i;

您创建了一个名为 product 的函数以及一个名为 product 的变量。
这使得几乎不可能引用正确的对象。处理您的名称管理,以便每个项目都明确无误。

【讨论】:

    【解决方案2】:

    您正在尝试调用 result 函数,您已为其提供了前向声明,但未提供实际实现。您需要为结果函数编写一个实现。这就是编译器抱怨未定义引用的原因。

    【讨论】:

    • 对不起,我说的当然是结果函数。将编辑!
    • #include int result(int v[], int size); int main(void) { int arr[5], n;对于 (n = -1; n
    • 这是所有语法错误的原始代码
    【解决方案3】:

    首先声明函数result

    int result(int v, int size);
    

    但未定义。

    另一方面,考虑到printf这个调用中的消息

    printf("the product of entered values is %d", result(n, 5));
    

    您的意思是一个计算数组元素乘积的函数,例如在main 之后定义的函数,但未使用

    int product(int a[]) {
        int product, i;
        for (i = 0; i <= sizeof(int); i++){
            product *= a[i];
        }
        return product;
    }
    

    所以让我们删除函数 result 的声明,并在 main 之前声明一个名称为 product 的函数,因为名称 result 的信息量不够。

    该函数处理一个数组。我们需要将数组中元素的数量传递给函数。由于数组本身不会在函数内更改,因此应使用限定符 const 声明它

    int 类型的整数的乘积也可能太大而无法存储在 int 类型的对象中。

    所以最好声明函数的返回类型至少像long long int(或者甚至像doublelong double)。

    因此函数声明看起来像

    long long int product( const int a[], size_t n );
    

    函数可以通过以下方式定义

    long long int product( const int a[], size_t n )
    {
        long long int result = n == 0 ? 0 : 1;
    
        for ( size_t i = 0; i < n; i++ )
        {
            result *= a[i];
        } 
    
        return result;
    }
    

    至于你的函数product然后你忘了初始化变量product

    int product, i;
    

    for循环中的条件

    for (i = 0; i <= sizeof(int); i++){
    

    没有意义,因为表达式 sizeof( int ) 不会产生数组中的元素数。

    尽量不要在此声明中使用像 5 这样的幻数

    int arr[5], n;
    

    在您的程序中。使用命名常量。

    数组的索引总是从0开始。所以这个循环

    for (n = -1; n < 4; n++){
    

    变量n 用作索引没有意义。

    还有这个 printf 调用

    printf("the product of entered values is %d", result(n, 5));
    

    必须放在 for 循环之外和函数 result 的调用中,该函数用作函数 printf 的参数

    result(n, 5)
    

    你甚至没有使用数组。

    因此程序可以如下所示

    #include <stdio.h>
    
    long long int product( const int a[], size_t n );
    
    int main(void) 
    {
        enum { N = 5 };
        int a[N];
        
        for ( size_t i = 0; i < N; i++ )
        {
            a[i] = i + 1;
        }
        
        printf( "The product of entered values is %lld", product( a, N ) );
        
        return 0;
    }
    
    long long int product( const int a[], size_t n )
    {
        long long int result = n == 0 ? 0 : 1;
    
        for ( size_t i = 0; i < n; i++ )
        {
            result *= a[i];
        } 
    
        return result;
    }
    

    程序输出是

    The product of entered values is 120
    

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多