0

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 be appreciated .

5
  • 1
    The first thing to learn is proper indenting of your code. Otherwise it's very difficult to read with all the code lined up on the left like that. Commented Dec 19, 2010 at 21:32
  • @david: You'll note the little toolbar icon's in the editor. The one marked {} renders a block as code, which is what Anon did to you question. There are other formatting options in the sidebar of the editing page. Commented Dec 19, 2010 at 21:33
  • yes it is homework, and i'm sorry i didn't knew how to mark the question,or i would've done it from the start. Commented Dec 19, 2010 at 21:40
  • Your subject should say more than just "Help with C Language" Commented Dec 19, 2010 at 21:44
  • point taken, and the subject changed Commented Dec 19, 2010 at 21:57

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It should be pointed out that when you do sizeof(arr)/sizeof(int) and arr has an array type, it does tell you how many elements are in the array. It's just that in this case src has a pointer type (int src[] being just another way to write int *src) and there's no way to pass an array to a function other than as a pointer.
0

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.

Comments

0

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.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.