如何在 C 中的数组中插入给定元素?

问题描述 投票:0回答:1

Here is the output to the code, the element actually gets inserted in the desired position but the initial value at that particular position in not translated to the next memory block, instead gets rewritten to the same value at the next block as seen in the image. 所以基本上,我无法弄清楚如何在 C 数组中向用户提供的给定位置添加元素(例如整数)。这里的数组是一维数组。

我尝试制作一个插入函数并将我的逻辑放入代码中,插入发生了,但问题是当它发生时,如果只是向右移动,该位置预先存在的值就会被覆盖。

// code to insert a specific value in an already existing array at any user specified position

#include <stdio.h>
void creation(int size, int position, int value);
void insertion(int arr[], int size, int position, int value);

int main()
{
    int size, position, value;
    printf("Enter the size of the parent array you wish to create - ");
    scanf("%d", &size);
    creation(size, position, value);
    return 0;
}

void creation(int size, int position, int value)
{
    int arr[size];
    for (int i = 0; i < size; i++)
    {
        printf("Enter the parent array element - ");
        scanf("%d", &arr[i]);
    }
    printf("Parent array created with elements \n");
    for (int i = 0; i < size; i++)
    {
        printf("%d \t", arr[i]);
    }
    printf("\n");
    insertion(arr, size, position, value);
}

void insertion(int arr[], int size, int position, int value)
{
    printf("Enter the position in the parent array where you would like to insert the element - ");
    scanf("%d", &position);
    if (position <= 0 || position > size+1)
    {
        printf("Invalid position entered, cannot add element! \n");
    }
    else
    {
        printf("Enter the value you wish to insert - ");
        scanf("%d", &value);
        for (int i = size-1; i >= position; i--)
        {
            arr[i+1] = arr[i];  // I THINK THIS IS WHERE THE PROBLEM IS ARISING IN THE FINAL ITERATION OF THE LOOP, THE ARRAY BECOMES
                                // BECOMES EMPTY WHICH IS WHY THE VALUE IS GETTING REPEATED
        }
        arr[position-1] = value;
        size += 1;
        printf("Array after insertion successfully \n");
        for(int i = 0; i < size; i++)
        {
            printf("%d \t", arr[i]);
        }
    }
}
arrays c insertion
1个回答
0
投票

所以这有很多问题。

首先,生成一个大小为

size
的数组。在 C 中,索引是从 0 开始的。这意味着数组的有效索引将为
arr[0]
arr[size-1]
。具体来说,
arr[size]
无效,尝试写入它会导致内存乱码(即,它是未定义的行为)。这很重要,因为你有循环:

        for (int i = size-1; i >= position; i--)
        {
            arr[i+1] = arr[i];
        }

注意第一次迭代,i=

size-1
,因此您将分配给
arr[size-1+1]
==> (
arr[size]
)。

接下来,在以下几行中:

        arr[position-1] = value;
        size += 1;
        printf("Array after insertion successfully \n");
        for(int i = 0; i < size; i++)
        {
            printf("%d \t", arr[i]);
        }

您增加了大小,但请注意,这不会使数组变大。为数组分配的空间保持不变。所以现在,当你执行 for 循环时,你再次循环到新的 size

 - 1,这又是一到远。

只要不插入值 - 您需要输入一些示例数据的输出来准确显示您面临的问题是什么。

最后,你的循环应该转到

position - 1

 (这是数组从 0 开始的索引)所以:

// v (note this is -2 here...) for (int i = size-2 ; i >= position-1; i--) { arr[i+1] = arr[i]; }
或者更好:

for (int i = size-1; i >= position; i--) { arr[i] = arr[i-1]; }
这会将数组元素移动到所需位置

© www.soinside.com 2019 - 2024. All rights reserved.