Static mapping declaration for C language

Source: Internet
Author: User
Tags printf

Some time ago, prepare to design a table about error information, each error has a unique Errid, and the corresponding error message and other auxiliary information. In the C language, it is natural to achieve the following:

enum {
Err_id_1,
Err_id_2,
Err_id_3,
Err_id_4
} Errid;
Const char* errmsg[] = {
"This is Error 1 msg",/*err_id_1*/
"This is Error 2 msg",/*err_id_2*/
"This is Error 3 msg",/*err_id_3*/
"This is Error 4 msg" /*err_id_4*/
};
int main () {
printf ("error=%s", Errmsg[err_id_1]);
return 0;
}

The problem with this is that it is not easy to maintain and must be artificially determined for each errid and its errmsg to correspond. Hope can not increase any time or space in the case of the cost, so that the realization of more "beautiful", easier to maintain. The first time you can think of is a macro or template.

Method One

#define ERR_MSG (ID, msg) const char* err_msg_# #id = msg;
#define GET_MSG (ID) (err_msg_# #id)

Err_msg (Err_id_1, "This is Error 1 MSG")
Err_msg (Err_id_2, "This is Error 2 MSG")
Err_msg (Err_id_3, "This is Error 3 MSG")
Err_msg (Err_id_4, "This is Error 4 MSG")

int main () {
printf ("error=%s", Get_msg (err_id_1));
return 0;
}

The advantage of this method is that the space of the errmsg array is omitted. Disadvantage is

It is not supported to dynamically determine output at run time through incoming errid.

ERRMSG traversal operations are not supported and do not even know the number of Errid. Because ErrMsg was not there.

Method Two

First, create a separate file, such as Err.txt, with each line in a format such as:

ERR_MSG(ERR_ID_1, "This is Error 1 msg")
ERR_MSG(ERR_ID_2, "This is Error 2 msg")
ERR_MSG(ERR_ID_3, "This is Error 3 msg")
ERR_MSG(ERR_ID_4, "This is Error 4 msg")

In the main file:

#undef err_msg
#define ERR_MSG (ID, MSG) ID,
enum {
#include "Err.txt"
Max_err_number
} Errid;

#undef err_msg
#define ERR_MSG (ID, msg) MSG,
Const char* errmsg[] = {
#include "Err.txt"
""
};

Add an unwanted element at the end to prevent some compilers from monitoring the warning when a comma is followed by the tail element.

Of course, errmsg arrays are not necessarily required. For example, you can implement a function that returns the corresponding errmsg via an incoming Errid get_err_msg ():

#undef err_msg
#define ERR_MSG (ID, msg) case Id:return MSG; Break
Const char* get_err_msg (Errid Eid) {
Switch (Eid) {
#include "Err.txt"
}
return NULL; Dummy. A warning used to eliminate some compilers.
}

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.