Arrays 如何使用函数在数组中插入元素?

Arrays 如何使用函数在数组中插入元素?,arrays,c,function,insert,Arrays,C,Function,Insert,我在用C编写一个函数时遇到了一些困难,它有两个参数,一个数组的名称和它的初始大小,并检查数组中是否有奇数。如果它找到一个,它将在它旁边插入它的double,并移动其余的元素。我知道在没有函数的情况下怎么做,但是有了函数,它就不起作用了。程序正在运行,但它什么也不做。请帮点忙好吗 例a[5]={2,5,6,8,11} 此外,函数不能返回任何内容,数组的索引范围为0到n-1 这就是我的函数的样子 void Insert(int v[], int *n) { int i,j;

我在用C编写一个函数时遇到了一些困难,它有两个参数,一个数组的名称和它的初始大小,并检查数组中是否有奇数。如果它找到一个,它将在它旁边插入它的double,并移动其余的元素。我知道在没有函数的情况下怎么做,但是有了函数,它就不起作用了。程序正在运行,但它什么也不做。请帮点忙好吗

例a[5]={2,5,6,8,11}

此外,函数不能返回任何内容,数组的索引范围为0到n-1

这就是我的函数的样子

void Insert(int v[], int *n)    
{
    int i,j;
    
    for(i=*n-1; i>=0; i--)     //passing through the array from right to left
    {
        if(v[i]%2==1)       // if the element is odd
        {
            *n++;           // grow the array size by 1 
            int double=v[i]*2;    
            for(j=*n-1; j>=i+1; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                v[j+1]=v[j];
            }
            v[i+1]=double;    // add the double value next space after the odd number.
        }
    }
}

如果分配给v的内存足够大,足以支持增长(参见主函数中的32),则存在两个问题:

double是一个保留字 *n++没有执行您需要的操作,该增量不是n指针的值 输出为:

2 5 10 6 8 11 22

问题:

double是c中的保留关键字。 实际上,从这个问题来看,我觉得你没有足够的内存来存储元素,而且你没有增加大小。 *n++相当于*n++,但您需要做的是*n++ 好的,如果内存足够大,可以存储所有元素,那么可以使用另一个答案。如果没有,您可以使用此代码:

然后,您需要使用realloc随时更改大小。因此,应该动态创建数组。您应该将指向数组int**的指针发送到函数,如下面代码所示。这些更改都是基于realloc的工作方式完成的

包括 包括 无效插入**v,int*n { int i,j; fori=*n-1;i>=0;i-//从右向左通过数组 { 如果*v[i]%2==1//如果元素为奇数 { 而1{ int*p=realloc*v,*n+1*sizeofint;//将数组大小增加1 如果p!=NULL{ *v=p; *n++; 打破 } 否则{ printfAllocation失败…重新分配\n; 持续 } } int var=*v[i]*2; forj=*n-1;j>=i+2;j-//将奇数右边的所有元素向右移动1个空格 { *v[j]=*v[j-1]; } *v[i+1]=var;//在奇数后的下一个空格中添加双精度值 } } } int main { int size=3; int*a=mallocsizeofint*size; forint i=0;i请解释您的具体问题或错误结果。似乎是另一个故事没有告诉我们任何具体的事情。double对于一个变量来说是一个非常糟糕的名字,甚至不确定它是否可以编译。你需要分配一些内存来增加一个数组。我没有在我的程序中使用double,但它是用我的母语翻译的,这被C语言所接受。然后我用英语翻译了我的整个程序,这样更容易理解,并且忘记了double是一种数据类型。所以基本上唯一的错误是我试图增加值的方式。编译器没有自满,所以我认为没问题。但我现在明白了。非常感谢你!这对我来说相当复杂,因为我还没有达到分配内存的主题。但是非常感谢你花时间回答我的问题。当我对这件事有了更好的了解后,我会回到这里,这会对我有更多的帮助。
void Insert(int v[], int *n)    
{
    int i,j;
    
    for(i=*n-1; i>=0; i--)     //passing through the array from right to left
    {
        if(v[i]%2==1)       // if the element is odd
        {
            *n++;           // grow the array size by 1 
            int double=v[i]*2;    
            for(j=*n-1; j>=i+1; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                v[j+1]=v[j];
            }
            v[i+1]=double;    // add the double value next space after the odd number.
        }
    }
}
static void Insert(int v[], int *n)
{
    int i,j;

    for(i=*n-1; i>=0; i--)     //passing through the array from right to left
    {
        if(v[i]%2==1)       // if the element is odd
        {
            (*n)++;           // grow the array size by 1
            int d=v[i]*2;
            for(j=*n-1; j>=i+1; j--)    // move all elements in the right of the odd number to the right by 1 space
            {
                v[j+1]=v[j];
            }
            v[i+1]=d;    // add the d value next space after the odd number.
        }
    }
}

int main(void) {
    int a[32]= {2, 5, 6, 8, 11};
    int len = 5;
    Insert(a, &len);
    for (int i = 0; i < len; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}