Home > Back-end >  C language check
C language check

Time:01-10

Can anybody check my code please? i couldn't find the error or the misbehavior of my code. Note that when i assign a value for x inside the program everything goes well, but if i want the user to assign a value for x, the program doesn't work properly. And sorry for my bad english. Thank you!

#include <stdio.h>
#include <string.h>

void sorting( int array[], int x){
    for (int i = 0; i < x-1; i  ) // The (x-1) is because we are starting at index zero!!
    {
        for (int j = 0; j < x-1; j  )
        {
            if (array[j]>array[j 1])
            {
                int temp;
                temp=array[j];
                array[j]=array[j 1];
                array[j 1]=temp;
            }
        }
    }
}
void printing(int array[],int x) {
    printf("\nThe sorting is:\n");
    for (int i = 0; i < x; i  )
    {
        printf("%d,\t",array[i]);
    }
}

int main(){
    int x;
    int array[x];

    printf("Enter the size of your array as a natural no:\n");
    scanf("%d",&x);

    printf("Now enter %d nos to be arrange:\n" ,x);
    
    for (int i = 0; i < x; i  )
    {
        printf("Enter a no:\n");
        scanf("%d",&array[i]);
    }
    printf("The array of %d int no:\n",x);
    for (int i = 0; i < x; i  )
    {
        printf("%d,\t", array[i]);
    }
    
    sorting(array,x);
    printing(array,x);

    return 0;
}

//I tried to assign a value for x inside the program everything goes well, but if i want the user to assign a value for x, the program doesn't work properly.

CodePudding user response:

The problem with your code is that you are declaring the array array with a variable size, which is determined by the value of x entered by the user. However, in C, arrays must have a fixed size at the time of declaration, and the size cannot be determined by a variable.

In your main() function, when you declare int array[x];, the size of the array is determined by the value of x, but x has not been initialized yet, so the size of the array is undefined. This can cause issues with accessing the elements of the array later on in the program, as well as other undefined behaviors.

A solution to this problem is to use dynamic memory allocation to allocate memory for the array at runtime.

int x; 
printf("Enter the size of your array as a natural no:\n");
scanf("%d",&x);
int* array = (int*)malloc(x*sizeof(int));

Another alternative is to use vector if your environmnet support C then you can use std::vector array(x);

It's important to remember that you need to free the memory that you've dynamically allocated when you're done with it.

free(array);

This way, you can use the value of x entered by the user to determine the size of the array, but the array will have a fixed size at the time of declaration.

Also, it would be better to use size_t to represent the size of array rather than int as it's a standard practice to use size_t for anything representing a size or index of something, it's a typedef for unsigned int, and guarenteed to be large enough to represent the maximum size of an object in memory.

It would be something like this:

size_t x;
printf("Enter the size of your array as a natural no:\n");
scanf("%d",&x);
int* array = (int*)malloc(x*sizeof(int));

Also, When you are done using array make sure to deallocate it.

free(array);

CodePudding user response:

You need to scan the x before array definition, otherwise x has undetermined value and you invoke Undefied Behaviour

int main(){
    int x;
    int array[x];

    printf("Enter the size of your array as a natural no:\n");
    scanf("%d",&x);

=====>

int main(){
    size_t x;

    printf("Enter the size of your array as a natural no:\n");
    if(scanf("%zu",&x) != 1 {/* handle scanf error */}

    int array[x];
  • Related