int funcc(int a[],int b[],int *cnt)
{
int *c;
int j,i,s=0;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(b[i]==a[j])
{
*cnt++;
break;
}
c=(int*)malloc(*cnt*sizeof(int));
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(b[i]==a[j])
{
c[s++]=b[i];
break;
}
return c;
}
void main (void)
{
int *c;
int *cnt=0;
int i,arr[n]={3,2,1},brr[n]={3,2,0};
c=funcc(arr,brr,&cnt);
for(i=0;i<*cnt;i++)
printf("%d ",c[i]);
}
I need to print the common numbers in 2 arrays.. but the problem is with "cnt" .. if i replace cnt with 3 it works .. but when i put cnt it doesnt work
Your prototype is:
int funcc(int a[],int b[],int *cnt)
but you are passing it a pointer to a pointer:
int *cnt=0; /* <- pointer */
int i,arr[n]={3,2,1},brr[n]={3,2,0};
c=funcc(arr,brr,&cnt); /* &cnt <- pointer to a pointer */
The problem is you declare cnt
as an int
pointer, so when you pass in &cnt
, you're passing in a pointer to a pointer to an int
. Try changing that second line of main to int cnt=0;
and change the for loop to for(i=0;i<cnt;i++)
(notice the removal of the *
character).
EDIT: the line *cnt++;
should be changed to either ++*cnt;
or (*cnt)++
, since the increment operator takes higher precedence than the dereference operator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.