如何用定义初始化结构?

时间:2018-07-11 17:02:09

标签: c struct

我在配置文件中有以下定义,并希望在编译时进行定义时自动初始化我的结构。我的目标是将myStruct.setting [0] .line [1] .number设置为5。但是在定义时出现错误。我该如何初始化它?

private void Click_Submit(object sender, EventArgs e) {
    foreach (ListViewItem lvi in listView1.SelectedItems)
        listView1.Items.Remove(lvi);

    if (string.IsNullOrWhiteSpace(textBox1.Text))
        MessageBox.Show("Please enter a serial number!", "Input");
    else
        InserSerialNumber(textBox1.Text);

    textBox1.Text = string.Empty;
    textBox1.Focus();
    textBox1.SelectionStart = textBox1.Text.Length == 0 ? 0 : textBox1.Text.Length - 1;
    textBox1.SelectionLength = 0;

    LoadSerialNumbers();
}
private void InsertSerialNumber(string sn) {
    using (OleDbCommand odc = new OleDbCommand("INSER INTO SN_Incoming(SN) VALUES(@SN)", con)) {
        odc.Parameters.AddWIthValue("@SN", sn);
        try {
            con.Open();
            odc.ExecuteNonQuery();
        } catch (Exception e) { MessageBox.Show(e.Message); } finally { con.Close(); }
    }
}
private void LoadSerialNumbers() {
    listView1.Items.Clear();
    DataTable dt = new DataTable();
    using (OleDbCommand odc = new OleDbCommand("SELECT * FROM SN_Incoming", con)) {
        try {
            con.Open();
            using (OleDbDataAdapter oda = new OleDbDataAdapter(odc))
                ida.Fill(dt);
        } catch (Exception e) { MessageBox.Show(e.Message); } finally { con.Close(); }
    }

    List<ListViewItem> items = new List<ListViewItem>();
    foreach (DataRow row in dt.Rows) {
        ListViewItem lvi = items.SingleOrDefault(s => s.Tag == row[1].ToString());
        if (lvi != null)
            continue;

        lvi = new ListViewItem(new string[] { row[0].ToString(), row[1].ToString(), row[2].ToString() });
        lvi.Tag = row[1].ToString();
        items.Add(lvi);
    }

    listView1.Items.AddRange(items.ToArray());
}

1 个答案:

答案 0 :(得分:2)

在转换为宏之前,先手动编写一个初始化程序。我用过:

#include <stdint.h>

typedef struct {
  struct {
    struct {
      char name[16];
      uint16_t number;
    } line[20];
  } setting[2];
} MyStruct_t;

MyStruct_t mst =
{                                   // structure as a whole
    {                               // setting
        {                           // setting[0]
            {                       // line
                { "name",   16 },   // line[0]
                { "number",  1 },   // line[1]
            }
        },
        {                           // setting[1]
            {                       // line
                { "zoo",   16 },    // line[0]
                { "bronx",  1 },    // line[1]
                { "new",    0 },    // line[2]
                { "york",   5 },    // line[3]
            }
        }
    }
};

#define MY_DEFINITION \
{\
    {\
        {\
            {\
                { "menu",     20 },\
                { "setting",   5 },\
            }\
        },\
        {\
            {\
                { "menu2",    50 },\
                { "setting2", 15 },\
            }\
        }\
    }\
}

MyStruct_t myStruct = MY_DEFINITION;

首先,我得到了没有宏要编译的初始化程序。然后,我重新编写了宏,以包含手动版本显示的必要的大括号。如果您确实有决心,可以研究一下从工作版本中删除花括号,但是最好使用完全花括号的初始化程序。

我可以说-这是一个可怕的结构!