//
Main.c
Pointer_search
//
Created by Ma C on 15/8/2.
Copyright (c) 2015 BJSXT. All rights reserved.
Requirements: To compare elements in two ordered arrays by pointer lookup, and to output the first identical element value in two arrays.
#include <stdio.h>
int *searchsameelement (int *a,int *b,int len1,int len2);
int main (int argc, const char * argv[])
{
int a[] = {5,11,8,6,7,10};
int b[] = {80,9,14,8,14,19,4};
The following method of calculating the length of an array is common within a function, but if you pass it as an argument to an external function, the array name degrades to a pointer, and the method below calculates the length of the array does not apply.
int len1 = sizeof (a)/sizeof (a[0]); //Calculate the length of array a
int len2 = sizeof (b)/sizeof (b[0]); //Calculate the length of array b
int *pt = searchsameelement (A,B,LEN1,LEN2); //Returns the first same value address
if (PT)
printf ("%d\n", *pt);
Else
printf ("The same number don not find!\n");
return 0;
}
int *searchsameelement (int *a,int *b,int len1,int len2)
{
int *temp = A; //Initialize temporary pointer to prevent temp from becoming a wild pointer
for (int i=0;i<len1;i++)
{
for (int j=0;j<len2;j++)
{
if (* (a+i) ==* (b+j))
{
temp = A+i;
Goto Loop;
//Here means find the first same element, jump to the end, return a+i
}
if (J==LEN2)
{
Break //indicates that the first outer loop number is not found in the inner loop, and jumps to the outer loop to continue looking for
}
}
if (i== (len1-1))
{
temp = 0;
Goto Loop;
//Here indicates that the inner and outer loops do not find the first same element, jump to the end, return 0
}
}
Loop:return temp;
}
C: Returns a pointer to the first identical element in two arrays (I used the loop, goto Loop label)