在赋值期间从数组位置获取错误的数据值。

时间:2022-09-02 21:23:46

I'm doing a programming exercise where I have to implement the fibonacci sequence using a loop (not recursive).

我正在做一个编程练习,我必须使用一个循环(而不是递归)来实现斐波那契序列。

While it's not perfect by any means, the main issue I have is with my algorithm.

虽然它并不完美,但我的主要问题是我的算法。

Because the variable intBuffer is assigned a pointer, it changes as the value of intArray[1] changes, rather than retain the previous value, before assigning it to memory location intArray[0].

因为变量intBuffer被指定了一个指针,它会随着intArray[1]的值的变化而改变,而不是保留以前的值,然后将其分配给内存位置intArray[0]。

I have tried using the * prefix to get a data value rather than a memory location, but this produced an error:

我尝试使用*前缀获取数据值而不是内存位置,但这产生了一个错误:

error: invalid type argument of unary '*' (have 'int')

错误:unary '*'的类型参数无效(有'int')

Code below:

下面的代码:

#include <stdio.h>
int main(void)
{
    int intArray[1];
    int intRounds, intIndex, intBuffer;
    printf("How many iterations would you like me to calculate? \n");
    scanf("%d", &intRounds);

    intArray[1] = 1;
    intArray[0] = 0;

    for (intIndex = 0; intIndex <= (intRounds - 1); intIndex++)
    {
        intBuffer = intArray[1];
        intArray[1] = intArray[1] + intArray[0];
        intArray[0] = intBuffer;
        printf("Iteration %d: %d \n", (intIndex + 1), intArray[1]);
    }   

    return 0;
}

1 个解决方案

#1


3  

Your code exhibits undefined behaviour, as you're defining intArray to have only one element and then you're trying to access 2nd element, by

您的代码显示了未定义的行为,因为您正在定义intArray只有一个元素,然后您尝试访问第二个元素,by。

 intArray[1] = 1;

C uses 0 based indexing for arrays, so for a one-element array, index 1 is an access beyond allocated memory and will invoke undefined behaviour.

C使用基于0的数组索引,因此对于一个元素数组,索引1是一个超出分配内存的访问,将调用未定义的行为。

That said, regarding the logic, inside the for loop, you're constantly overwriting the value at intArray[i].

这就是说,对于逻辑,在for循环中,您总是在intArray[i]上重写值。

You should

你应该

  1. Define an array large enough to hold all the values of the series length. You can make use of a pointer and malloc() for dynamic memory allocation, or VLA (if supported).
  2. 定义一个足够大的数组来容纳序列长度的所有值。您可以使用指针和malloc()来进行动态内存分配,或者VLA(如果支持的话)。
  3. Use the loop counter intIndex to index the target element in each iteration.
  4. 使用循环计数器intIndex在每次迭代中索引目标元素。

#1


3  

Your code exhibits undefined behaviour, as you're defining intArray to have only one element and then you're trying to access 2nd element, by

您的代码显示了未定义的行为,因为您正在定义intArray只有一个元素,然后您尝试访问第二个元素,by。

 intArray[1] = 1;

C uses 0 based indexing for arrays, so for a one-element array, index 1 is an access beyond allocated memory and will invoke undefined behaviour.

C使用基于0的数组索引,因此对于一个元素数组,索引1是一个超出分配内存的访问,将调用未定义的行为。

That said, regarding the logic, inside the for loop, you're constantly overwriting the value at intArray[i].

这就是说,对于逻辑,在for循环中,您总是在intArray[i]上重写值。

You should

你应该

  1. Define an array large enough to hold all the values of the series length. You can make use of a pointer and malloc() for dynamic memory allocation, or VLA (if supported).
  2. 定义一个足够大的数组来容纳序列长度的所有值。您可以使用指针和malloc()来进行动态内存分配,或者VLA(如果支持的话)。
  3. Use the loop counter intIndex to index the target element in each iteration.
  4. 使用循环计数器intIndex在每次迭代中索引目标元素。