简体   繁体   中英

How to increase and decrease array in C language with realloc function?

I wrote a function that receives a set of grades from the user, it should ask the user a new size of array and this will increase the array and allow the user to add organs or reduce the array and delete organs.

But instead it only changes the array locally and also prints it. How can I get her to return the array and cancel the unnecessary print?

Function:

    void changeNumber(int *grades, int size)
    {

    int newSize = 0;
    int* newGrades = 0;
    int i = 0;

    printf("Enter new number of grades: ");
    scanf_s("%d", &newSize);

    if (newSize > size)
    {
        grades = (int*)realloc(grades, newSize * sizeof(int));
        for (i = size; i <= (newSize-1); i++)
        {
            printf("Enter grade %d: ", i+1);
            scanf_s("%d", grades+i);
            grades[i] = checkNumber(grades[i]); 
        }
        for (i = 0; i < newSize; i++)
        {
            printf("%d ", grades[i]);
        }
    }
    else if (newSize < size)
    {
        newGrades = (int*)malloc(newSize * sizeof(int));
        for (i = 0; i < (newSize-1); i++)
        {
            grades[i] = grades[i];
        }
        for (i = 0; i < newSize; i++)
        {
            printf("%d ", grades[i]);
        }
    }
    free(newGrades);

If the function return type is void (you are not returning the new pojnter to a dynamically allocated array) then you have to pass the pointer to the function by reference

void changeNumber(int **grades, int size);

Otherwise the function will deal with a copy of the original argument and changes of the copy do not influence on the original argument.

But as the function sets a new size of the array then the user of the function should know what is the new size of the array. So you should for example return from the function the new size of the array.

int changeNumber(int **grades, int size);

Also if the new allocated array has a size that is less than the current size then there is no need to copy elements to themselves

    newGrades = (int*)malloc(newSize * sizeof(int));
    for (i = 0; i < (newSize-1); i++)
    {
        grades[i] = grades[i];
    }

And you have to assign the new pointer to the old pointer to the first element of the array.

Instead of the type int used for the size of the array it is much better to use the unsigned integer type size_t .

The function can look for example the following way

size_t changeNumber( int **grades, size_t size )
{
    size_t newSize = 0;

    printf( "Enter new number of grades: " );

    if ( scanf( "%zu", &newSize ) == 1 && size != newSize )
    {
        int *newGrades = realloc( *grades, newSize * sizeof( int ) );

        if ( newGrades != NULL )
        {
            if ( size < newSize )
            {
                for ( size_t i = size; i < newSize; i++ )
                {
                    printf( "Enter grade %zu: ", i + 1 );
                    scanf("%d", newGrades + i );
                    newGrades[i] = checkNumber( newGrades[i] ); 
                }
            }

            size = newSize;
            *grades = newGrades; 
        }
    }

    for ( size_t i = 0; i < size; i++ )
    {
        printf( "%d ", ( *grades )[i] );
    }
    putchar( '\n' );

    return size;   
}

Pay attention to that the pointer that is indirectly passed to the function through pointer to it as an argument also must point to a dynamically allocated array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM