C预处理器init数组

时间:2017-07-26 11:18:20

标签: c c-preprocessor

我正在编写一个复杂的宏,我还需要传递数组初始化器。基本上我很难做到:

#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = ARR_DATA

然后我会称之为

INIT_ARR(myNm,{1,2,3});

但预处理器将任何逗号(也是花括号内的逗号)解释为新的宏参数,因此它给出了错误:

error:  #55-D: too many arguments in invocation of macro "INIT_ARR" 

预处理器不会忽略()所以我可以这样做:

#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = {ARR_DATA}
INIT_ARR(myNm,(1,2,3));

但它被解释为

int myNm[] = {(1,2,3)};

这对于C.

是不正确的

有办法怎么办?例如,从参数?

中删除大括号

2 个答案:

答案 0 :(得分:7)

我想我破解了它:

#define myArgs(...) __VA_ARGS__
#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = {myArgs ARR_DATA}
INIT_ARR(myArr,(1,2,3,4));

将被正确解释为:

int myArr[] = {1,2,3,4};

annoying_squid's answer让我弄清楚了......

答案 1 :(得分:6)

您可以将宏的可变数量的参数用作 -

        int Quantity;
        string JobName;
        string OrderType;
        DateTime DueDate;

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);

        string cmdText = "SELECT Quantity,Job_Name,[Order],Due_Date FROM Shopping_Cart WHERE UID=@UIDD";

        SqlCommand cmd = new SqlCommand(cmdText, con);

        //===== Adding parameters/Values.
        cmd.Parameters.AddWithValue("@UIDD", hfUserID.Value);

        //===== To check current state of the connection object. If it is closed open the connection
        //===== to execute the insert query.
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        //===== Execute Query.
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();

        Quantity = dr.GetInt32(0);
        JobName = dr.GetString(1);
        OrderType = dr.GetString(2);
        DueDate = dr.GetDateTime(3);

        con.Close();
        if (Session["UID"] != null)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] {
                            new DataColumn("QUANTITY", typeof(string)),
                            new DataColumn("JOB NAME", typeof(string)),
                            new DataColumn("ORDER TYPE", typeof(string)),

                            new DataColumn("DUE DATE", typeof(string))});

            dt.Rows.Add(Quantity, JobName, OrderType, DueDate);

  YourTable.Append("<table border = '1'>");
            YourTable.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                YourTable.Append("<th style = 'background-color: #0bd2d1;color:#ffffff'>");
                YourTable.Append(column.ColumnName);
                YourTable.Append("</th>");
            }
            YourTable.Append("</tr>");
            foreach (DataRow row in dt.Rows)
            {
                YourTable.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    YourTable.Append("<td>");
                    YourTable.Append(row[column]);
                    YourTable.Append("</td>");
                }
                YourTable.Append("</tr>");
            }