C: Returns a pointer to the first identical element in two arrays (I used the loop, goto Loop label)

Source: Internet
Author: User

//

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.