C language interface and implementation experiment-table (table_t)

Source: Internet
Author: User

The experimental program is compiled with vc6, pay attention to the file extension is C, not CPP, In the download: http://download.csdn.net/detail/dijkstar/4036742

Notes and problems encountered are already in the Implementation Program annotations, as shown below:

# Include <stdio. h> # include <stdlib. h> // use the rand function # include <string. h> # include "include/table. H "# include" include/atom. H "# pragma comment (Lib," libcii. lib ") // generate a random number int my_rand (int m, int N) {return (INT) (double) rand () in the [M, N] interval () /(double) rand_max * (N-M + 1) + M);} // traverses the output function void print (const void * Key, void ** Val, void * cl) {char * k = (char *) Key; int ** v = (INT **) val; printf ("% s --- % d \ n", K, * V);} // traverse and modify the function // Simple Repair Change to a fixed value void edit (const void * Key, void ** Val, void * Cl) {int ** v = (INT **) val; * V = 10000;} void main () {// note: the C language must place these variable declarations in the function header table_t T1; int I = 0; char tmpkey [100] = {0}; char * Key = NULL; intval; void ** ary; int tmp1 = 0, * TMP = & tmp1; // create table T1 = table_new (0, null, null); // Add data // Note: Data for (; I <10; I ++) {sprintf (tmpkey, "Key % 02d", I); // generate a temporary [Key] val = my_rand (1000,999 9 ); // generate [Val] Key = atom_st Ring (tmpkey); // the address of the second [Key] must be an "atom" (that is, the address of each string cannot be the same !!) // Do not simply pass a string address, because the string address is different from the "Atomic" address // when the third item [Val] is passed, refer to the source code implementation of the function-the address value is recorded, instead of copying the content of the address to the table. // The simple value passed here is an int. If it is a struct variable, it should also be to pass the address value of the struct variable directly in, // re-emphasize that table_put directly assigns an address value internally, rather than copying the content on this address !! Table_put (T1, key, Val);} // output table length printf ("table length = % d \ n", table_length (T1 )); // return an item in the table // note again: do not simply pass a string address, but pass an atomic address val = (INT) table_get (T1, atom_string ("key03"); printf ("Return: key03 = % d \ n", Val); // modify the returned Val value. The value cannot be modified, verify again that the Val passed by this test program to the table is a value, not the address (pointer) * TMP = (INT) table_get (T1, atom_string ("key03 ")); * TMP = 10000; val = (INT) table_get (T1, atom_string ("key03"); printf ("cannot be modified: key03 = % d \ n", Val ); // output table method 1 prin TF ("output Table Method 1: table_toarray \ n"); Ary = table_toarray (T1, null); for (I = 0; Ary [I]; I + = 2) {// The second item is simply an int, which is what it looks like when it is passed !! Printf ("% s --- % d \ n", (char *) ary [I], (INT) ary [I + 1]);} // output table method 2 printf ("\ N output Table Method 1: table_map \ n"); table_map (T1, print, null); // Delete val = (INT) table_remove (T1, atom_string ("key03"); printf ("deleted key03 = % d \ n", Val); // method 2 of output table again, check if "key03" exists? Printf ("\ n confirm" key03 "does not exist \ n"); table_map (T1, print, null); // traverse and modify table_map (T1, edit, null ); // output table method 2 again. Check whether each item modifies printf ("\ n confirm that each item has been modified \ n"); table_map (T1, print, null );}

Output result:

Table length = 10 search return: key03 = 8278 OK cannot be modified: key03 = 8278 output Table Method 1: table_toarraykey03 --- 8278key04 --- 6265key05 --- 5318key06 --- 4102key07 --- 9063key08 --- 8405key09 --- 7719key00 --- 1011key01 --- 6072key02 --- 2739 output Table Method 1: keys --- keys --- 5318key06 --- 1272key07 --- 9063key08 --- keys --- 7719key00 --- 1011key01 --- 6072key02 --- 2739 Delete key03 = 8278 confirm "key03" does not exist key04 --- keys --- 5318key06 --- 20172key07 --- 9063key --- keys --- 7719key00 --- 1011key01 --- 6072key02 --- 2739 confirm that each item has modified key04 --- 127key05 --- 127key06 --- 127key07 --- 127key08 --- 127key09 --- 127key00 --- 127key01 --- 127key02 --- 10000 press any key to continue

Through this experiment, we deeply understand the "uniqueness" and address pointer of the atom.

Note: ① The key used in the experiment program is an atom. According to the table function interface, it is a void * type. Therefore, an int, a char, and so on can be passed as the key ];

② If it is unclear, you can track the source program implementation to understand what is going on. For example, two parameters following table_new (), one is a comparison function and the other is a hash function, which can be passed as null. An internal function is used.

③ Table_put function, the third parameter (the last parameter) is a void * val, which is a pointer type. However, note that when calling this function, it is not the content on this address, copy it to an internal table, but write down the address. We also think of the following functions:

CComboBox::SetItemDataCListBox::SetItemDataCTreeCtrl::SetItemData

Use these functions to understand the table_put function.


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.