开发者

how to change one array into another while canceling the recurring variables?

开发者 https://www.devze.com 2023-01-31 14:08 出处:网络
I\'m new to programing and was given a task of making a function that puts one array into the other with the following criteria: a variable in the destination array will repeat only once and the sourc

I'm new to programing and was given a task of making a function that puts one array into the other with the following criteria: a variable in the destination array will repeat only once and the source and destination array will be of the same size. the function i came up with is:

int RemoveDup (int src[],int dst[]) 
//recive two array compare them and copy the src array to dst,and only the none reacuring 
//numbers,the arrays must be from the same size

{
int size_src;
int size_dst;
int i,n=0;
size_src = sizeof(src)/sizeof(int);//determine the size of source array
size_dst = sizeof(dst)/sizeof(int);//determine the size of destination array
if (size_src = size_dst);//checks that the array are in the same size
{
for(i = 0;i < size_src;i++)//the loop for advancing the copying process
{
dst[i] = src[i];
}
while (i<size_dst)
{
dst[i] = dst[i++];

if (dst[i] = dst[i++])//relay on the fact that if the function will find a similar varibale, the tested varibale will be set to 0 and the other one will come out clean in the check
dst[i] = 0;//eliminating the varibale in that specific address
}
}


return dst [i];

but it doesn't seems to work and have no idea where it is going wrong. any help or clue will b开发者_JAVA技巧e appreciated .


I noticed that you're using sizeof(src) within a function that takes int src[] as a parameter. This is not doing what you think it is doing. In C, the size of arrays is not passed to functions along with the array itself (unlike some other languages you may be familiar with). You will have to pass the actual size as a separate parameter.

Also, some printf() statements will definitely help your debugging efforts. Make sure values are what you think they should be. Hopefully you have access to an interactive debugger, that would probably be really useful for you too.


In C you cannot declare a function that takes a parameter that is an array. When you use an array declarator as a function parameter the type is silently adjusted to the corrsponding pointer type. Any explicit array size (if specified) is discarded.

In other words, when you use this:

int RemoveDup (int src[],int dst[])

it is exactly equivalent to this:

int RemoveDup( int *src, int *dst )

It should now be obvious why sizeof(src)/sizeof(int) doesn't do the calculation that you wanted it to do.


well in fact it is possible to make a function recieve an array and not a pointer (given of course the size of the array is pre-defined).

so you can use:

int RemoveDup(int src[M],int dst[N]){
     .
     .
     .
return whatever;

I will agree though that using pointers is better. In my opinion you should write a recursive function to do that using of course pointers. so that the next call is (*src+1) so you look at the next cell. the exit condition is then:

if (sizeof(src) == 0) {
 //exit recursion statement.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消