(c language) string comparison function, pointer array and array pointer

Source: Internet
Author: User

Problem Description:

Write a function that compares the comparison of two strings (String_compare).

Program Analysis:

(1) Main idea: after passing two strings, compare each element in the two strings, and if the first comparison is not equal, do not let it enter into the comparison below. This returns a subtracted value (that is, the two elements in the two arrays that start unequal, the return value (int type), and the ASCII value subtraction). When entering the comparison process, the equality returns 0, and all other cases return the value that is subtracted.

(2) The main way: define an array of pointers and initialize them. Then, according to the above ideas, the implementation of the Code.

The code is as follows:

/*** pointer Array (1) int *a[10] is an array of pointers---> is an array (the elements in each array are int* types) (2) int (*a) [10] is an array pointer---> points to an array (an array of 10 int types) NOTE: *,[] , the () priority is incremented in turn. The following is an example of an array of pointers, as for array pointers: **/#include <stdio.h> #include <assert.h>int string_compare (const char *str1,const char *str2) {assert (str1 );  ASSERT (f) assert (STR2);/* In debug mode, the expression in parentheses is evaluated each time it is run, and if the expression is 0, the execution is interrupted, a warning box pops up, the user can select "Continue", "retry", "ignore" in release mode, This statement is not compiled into code. Assert is generally used to verify the correctness of the parameters inside the program, that is, when the internal function is called, the caller must ensure that the parameters are correct, and the inside of the called function, you can check whether the parameter satisfies the requirement by assert. */while (*str1 = =  *str2)//Determine whether the elements in two arrays are equal {str1++;  Make two pointers pointing to the next bit; continue to compare str2++;if (*str1 = = ')  ///Both sides compare to ' "", all equal {return 0;   Returns 0, representing two strings equal}}return *str1-*str2; If not equal, returns a positive or negative value}int main () {char *ch[2];ch[0] = "AB";//initializes the element of this pointer array to the first address of the first element of the string ch[1] = "a";//Ditto printf ("%d\n", String_compare (ch[0],ch[1]));//Call this function return 0;}



(c language) string comparison function, pointer array and array pointer

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.