I was attempting to recreate the fibonacci sequence and I made a working code but my code will refuse to use a number above 12. any ideas what is wrong?

#include <stdio.h>
#include <stdlib.h>

int main()
{

    const int user = 1;
    long int fib[user];
    int i = 0;
    int pastpast = 0;
    int past = 0;

    printf("how many numbers should i print?:");
    scanf("%d", &user); //asks how many numbers to do

    for(i = 0; i < user; i++)
    {
        fib[i] = 0;
    }   // sets array to 0

    i=0;
    while(i != user)
    {
        if(i == 0)
        {
            fib[i] = 1;
            i++;
        }
        if(i == 1)
        {
            fib[i] = 1;
            i++;
        }   //sets the first and second numbers to 1
        pastpast = fib[i-2];
        past = fib[i-1];
        fib[i] = past + pastpast;       
        i++;    // goes through the sequence
    }

    for(i = 0; i < user; i++)
    {
        printf("%d,", fib[i]);
    }   //prints all the numbers

    printf("\n");

    return 0;
}

Recommended Answers

All 6 Replies

Your problem is here: long int fib[user]; on line 8. You have sized the array at 1 (the value of the 'user' variable). The fact that it doesn't segfault on you more quickly is probably accidental. In any case, you are munging the stack. Instead, you should make the variable 'fib' a pointer, and then allocate it after you accept the user input, as in long int* fib = calloc(user, sizeof(long int));.

Using calloc() instead of malloc() allows you to skip setting the array elements to 0.

arrays are the same thing as pointers(arrays are a series of pointers), but i did take your suggestion placing my pointer line below user input and made the corrections. the same error occurs.

I am compiling using an ubuntu server

Show your current code please. And no, arrays are NOT a series of pointers. They ARE a pointer, to the first element of the array. IE:

int array[] = {0,1,2,3};
int *parray1 = array;
int *parray2 = &array[0];

/* parray1 should == parray2 */

In any case, the statement int array[1]; is declaring (and defining) that 'array' is an array of integers with ONLY one element, and IT CANNOT BE RESIZED! It is fixed in size. Setting an index > 0 on this array will result in "undefined behavior", which usually means a segfault...

arrays are the same thing as pointers(arrays are a series of pointers)

Wrong on both counts. Arrays are not pointers, period. There are two situations where C can trick you though:

  1. In value context (which happens to be most of the time), an array name will be converted to a pointer to the first element. The very fact that sizeof when used on an array will give you the total bytes taken by the array rather than the bytes in a pointer is enough to prove that arrays are not pointers. The operand to sizeof is used in object context, not value context, and it's one of the places where the conversion doesn't happen.

  2. In a function signature (barring C99's VLAs and advanced array notation), the first dimension of an array parameter is internally converted to a pointer and the size is ignored. So these three signatures are functionally identical:

    void foo(int a[10]);
    void foo(int a[]);
    void foo(int *a);

Let's look at some problems with your code:

long int fib[user];

C doesn't support using const variables as the size of an array because const doesn't mean constant, it means read-only. That's a subtle but significant difference. It's possible you're using C99's variable length array or a compiler extension, but it's equally likely that you're accidentally compiling as C++.

scanf("%d", &user); //asks how many numbers to do

user is a const variable. This scanf() call is just asking for trouble.

Really all you need to do to make this code work is give the fib array a proper size and make user non-const. For the size I'd recommend 46, since that's the largest fibonacci number you can fit into a signed 32-bit entity. You might consider checking the user input as well to make sure it's in range:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int fib[46];
    int user;
    int i = 0;
    int pastpast = 0;
    int past = 0;

    printf("how many numbers should i print?:");
    scanf("%d", &user); //asks how many numbers to do

    if (user < 0) {
        user = 0;
    }
    else if (user > 46) {
        user = 46;
    }

    for(i = 0; i < user; i++)
    {
        fib[i] = 0;
    }   // sets array to 0

    i=0;
    while(i != user)
    {
        if(i == 0)
        {
            fib[i] = 1;
            i++;
        }
        if(i == 1)
        {
            fib[i] = 1;
            i++;
        }   //sets the first and second numbers to 1
        pastpast = fib[i-2];
        past = fib[i-1];
        fib[i] = past + pastpast;       
        i++;    // goes through the sequence
    }

    for(i = 0; i < user; i++)
    {
        printf("%d,", fib[i]);
    }   //prints all the numbers

    printf("\n");

    return 0;
}

C doesn't support using const variables as the size of an array because const doesn't mean constant, it means read-only. That's a subtle but significant difference. It's possible you're using C99's variable length array or a compiler extension, but it's equally likely that you're accidentally compiling as C++.

are there any other suble changes from C++ to C? like, does "static" still work in front of a variable and can you just make a variable outside of a function to make it global?

sorry if this is off topic for you or you would encourage myself finding out on my own.

are there any other suble changes from C++ to C?

Yes. Click Here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.