Home > Back-end >  C: Problem with number 5 as length of array
C: Problem with number 5 as length of array

Time:12-16

My friend made a program in C that gets 2 inputs. One being the length of the array and the other being the elements of the array. The code works fine, except for when the length of the array is 5. When the length of the array if 5, the first element becomes the same as the last element in the array (e.g. if the elements were 1, 2, 3, 4, 5, then it would be switched to 5, 2, 3, 4, 5). Here is the code he wrote:

#include <stdio.h>

int main()
{
    int length;

    printf("What is the length of your array?: ");
    scanf("%d", &length);

    length -= 1;
    int X[length], Y[length];

    printf("What are the elements of your array?: \n");

    for (int i = 0; i <= length; i  )
    {
        scanf("%d", &X[i]);
        Y[i] = X[i];
    }

    printf("\n");

    for (int i = 0; i <= length; i  )
    {
        printf("%d ", X[i]);
    }

    printf("\n");

    for (int i = length; i >= 0; i--)
    {
        printf("%d ", Y[i]);
    }
}

I tried searching the internet, but no matter what I do, I can't really wrap my head around what's happening.

CodePudding user response:

You appear to be confusing length and offset. length -= 1 will result in the allocation of X and Y to be too small, so when you iterate backwards through Y you’ll likely end up addressing the location of the last element of X. The reason you see it with 5 elements is likely to be due to 32 bit alignment in your compiler (4 bytes). You’ll likely see the same at 9, 13 etc.

But that’s very dependent on compiler/platform in terms of memory alignment. Anything where the offset exceeds the bounds of the array will lead to undefined behaviour.

If you move length -= 1 after the declaration of X and Y it should work, but this isn’t generally good/preferred practice.

Alternatively remove length -= 1 and change the comparison against length in the loops to be i < lengths, and int i = length - 1` in the last loop. This is generally the preferred style in C.

CodePudding user response:

I think the way allocating memory without declaring values is not working properly. Whenever you don't yet know the size, declare without initialising the array, and afterwards assing a memory of the size as you know it (with malloc)!

int length;

printf("What is the length of your array?: ");
scanf("%d", &length);
length -=1;
//int X[length], Y[length];
int *X, *Y;
X,Y = (int *) malloc(length);

After that, with 5:

What is the length of your array?: 5
What are the elements of your array?: 
1
2
3
4
5

1 2 3 4 5 
5 4 3 2 1 

CodePudding user response:

The size of array is not enough.

According to your code:

// Input length is 5
length -= 1; // The length is 4
int X[length], Y[length];
for (int i = 0; i <= length; i  )  // i -> 0 to 4, loop 5 times, but size of array is 4
    {
        scanf("%d", &X[i]);
        Y[i] = X[i];
    }

so swap two rows, it is correct.

int X[length], Y[length];
length -= 1;
  • Related