如何在c中将一个数组的元素追加到另一个数组中 [英] how to append the elements of one array to another in c

查看:521
本文介绍了如何在c中将一个数组的元素追加到另一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个数组

input1 = [1,2,3,4]

input2 = [5,6,7,8]

input3 = [9,10,11]



i想制作一个输出阵列[1,2,3,4,5,6,7,8 ,9,10,11]

size如果thr array1 + array2 + array3;

I have 3 array
input1=[1,2,3,4]
input2=[5,6,7,8]
input3=[9,10,11]

i would like to make one outputarray[1,2,3,4,5,6,7,8,9,10,11]
size if thr array1+array2+array3;

int *combinearray(int *input1,int *input2,int *input3,int leninput1,int leninput2,int leninput3,int totallen)
{
		
	int i,j,k;
	int *output;
	output=(int*)malloc(totallen*sizeof(int));
		for(i=0;i<totallen;i++)
			{
				//if(i>=0&&i<=leninput1)
				output[i]=input1[i];
				j=0;
				if(i>=leninput1+1&&i<=leninput1+leninput2)
				{
					output[i]=input2[j];
					j++;
				}
				else
				k=0;
				{
					output[i]=input3[k];
					k++;
				}
			}
return output;
}





但输出我无法获得,因为我的目标是谁能告诉我我哪里错了?



but output i am not able to get as i am aiming for can anyone tell me where i wen wrong?

推荐答案

由于数组基于连续的内存片段,因此不适合附加。你实际上并没有试图追加,而是创造了一个全新的阵列,这很好。



然而,返回阵列是一个错误的想法,以及 totallen 。为什么不返回阵列?您创建了函数与内存分配的不必要的耦合,这限制了函数的使用,更糟糕的是,您隐藏了函数内的分配并将释放留给调用者。这不仅限制太多,这也很糟糕。如果调用者释放内存,它也应该分配它。此外,调用者应该能够自由选择阵列存储器的位置。它可以是堆或堆栈,或通过其他机制获得的内存,任何东西。

所以,让你签名

As arrays are based on continuous fragments of memory, there are not suitable for appending. You are actually not trying to append, but are creating a brand-new array, which is good.

However, returning the array is a wrong idea, as well as totallen. Why not returning array? You create unwanted coupling of your function with the memory allocation, which limits the use of your function, and, much worse, you hide allocation inside function and leave deallocation to the caller. Not only this is too limiting, this is just bad. If the caller deallocates memory, it should allocate it, too. Besides, the caller should be able to freely choose the location of array memory. It could be heap or stack, or memory obtained by some other mechanism, anything.
So, make you signature
void mergeArrays(
   int* input1, int length1,
   int* input2, int length2,
   int* input3, int length3,
   int* output);



调用者应该为所有四个指针提供足够的结果空间( sizeof(int)*(lenght1 + lenght2 + lenght3)任何类型的内存中的字节);总和应该在你的函数内计算。该函数需要假设所有长度都正确指定。



-SA


The caller is supposed to provide all four pointer with enough room for result (sizeof(int)*(lenght1 + lenght2 + lenght3) bytes) in any kind of memory; and the total sum should be calculated inside your function. The function needs to assume that all lengths are specified correctly.

—SA


你的方法虽然它可以工作很慢并且容易出错。



尝试类似:



Your approach while it can work is slow and prone to errors.

Try something like:

int *combinearray(int *input1,int *input2,int *input3,int leninput1,int leninput2,int leninput3)
{

    int *output = NULL;
    output=(int*)malloc((leninput1+leninput2+leninput3)*sizeof(int));

    if(output)
    {
        memcpy(output, input1, leninput1 * sizeof(int));
        memcpy(output + leninput1, input2, leninput2 * sizeof(int));
        memcpy(output + leninput1 + leninput2, input3, leninput3 * sizeof(int));
    }

return output;
}


这篇关于如何在c中将一个数组的元素追加到另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆