Implementation of generic programming in C Language (1)

Source: Internet
Author: User

Generic programming allows you to write fully generalized and reusable algorithms, with the same efficiency as the algorithms designed for a specific data type. In C language, you can use some methods to implement such generic programming. Here we will introduce a method -- using void with no type pointer *

Let's take a look at the following swap function that implements the exchange of two elements. An integer int is used as an example:

 swap(* i1,*= **i1 = **i2 =

When you want to exchange two char Types, You have to rewrite a function whose parameter type is char. Can we use a non-type pointer as a parameter? See the following changes:

 swap( *vp1, * temp = **vp1 = **vp2 =

This piece of code is incorrect and cannot be compiled. First, the variable cannot be declared as void of no type. However, you do not know the type of parameters passed in by calling this function, and cannot determine a type of declaration. At the same time, * cannot be used on a non-type pointer, because the system does not have the information that this address points to the object size. During the compilation phase, the compiler cannot know the type of the input function parameter. To implement a generic function, you need to input the address space size of the object to be exchanged at the place where the function is called, and use the string in the header file. the memcpy () function defined in h. The changes are as follows:

 swap( *vp1, *vp2, buffer[size];

When calling this function, it can be called as follows (also applicable to other types of x and y ):

 x = ,y = &x,&y,());  

The following describes another function:

 lsearch( key, array[],( i = ;i < size; ++(array[i] == -

This function searches for the key element in the array and returns its index. If-1 is not found, the returned result is-1. As shown above. You can also implement a generic function:

* lsearch(* key,  *,  n, ( i = ;i < n; ++ *elemAddr = ( *)+i*(memcmp(key, elemAddr, elemSize) == 

Function memcmp () prototype: int memcmp (void * dest, const void * src, int n ), compare the content in the address space where the two segments have n headers: dest and src.

This function searches for the key element in the array base. if it finds the key element, its address information is returned. If it cannot find the key element, NULL is returned.

If you use a function pointer, You can implement the generic type of its behavior:

 *lsearch( *key, *, n, elemSize,(*cmpfn)(*,*,( i = ;i < n; ++ *elemAddr = ( *)+i*(cmpfn(key,elemAddr,elemSize) == 

 intCmp(* elem1,** ip1 =* ip2 = *ip1-*

See the following call:

 array[] = {,,,,, size =  number =  *found = lsearch(&number,array,size,((found ==);

Related Article

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.