[英]How do I declare and use an array of struct in a for loop?
I am working on a native winapi application using cpp and sqlite http://github.com/jacksiro/vsongbook-cpp using the falcon c++ IDE. 我正在使用cal和使用falcon c ++ IDE的sqlite http://github.com/jacksiro/vsongbook-cpp开发本机winapi应用程序。 This is my database: 这是我的数据库:
void CreateDatabase()
{
sqlite3 * db = NULL;
int db_qry;
const char * sqlCreateTables =
"CREATE TABLE my_friends(friend_name VARCHAR(20), "
"friend_job VARCHAR(20), friend_job INTEGER(11));";
db_qry = sqlite3_open("friends.db", &db);
db_qry = sqlite3_exec(db, sqlCreateTables, NULL, NULL, NULL);
sqlite3_close(db);
}
And the database I insert it with this: 而数据库我将其插入:
void InsertToDatabase()
{
int db_qry;
char *error;
sqlite3 *db;
const char *sqlInsert =
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Ngunjiri James', 'Teacher', 25);"
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Wafula Shem', 'Capernter', 30);"
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Jane Akinyi', 'Nurse', 23);";
db_qry = sqlite3_open("friends.db", &db);
db_qry = sqlite3_exec(db, sqlInsert, NULL, NULL, &error);
sqlite3_close(db);
}
Now I have a simple listbox in my window where I populate it with values from my sqlite table like this. 现在,我在窗口中有一个简单的列表框,在其中用这样的sqlite表中的值填充它。
inline UINT AddStringList(const HWND hList,const ustring& s)
{
return static_cast<UINT>(SendMessage(hList,LB_ADDSTRING,0,
reinterpret_cast<LPARAM>(s.c_str())));
}
//I call this method in my WM_CREATE in WinMain and it works very well
void AddFriendListBox(const HWND hList)
{
sqlite3 * db = NULL;
sqlite3_stmt * stmt;
int i, db_qry;
const char * tail;
const char * sqlSelect = "SELECT * FROM my_friends";
db_qry = sqlite3_open("friends.db", &db);
if(sqlite3_prepare(db, sqlSelect, -1, &stmt, &tail) == SQLITE_OK)
{
while(sqlite3_step(stmt) != SQLITE_DONE)
{
for(i = 0; i < sqlite3_column_count(stmt); i++) {
const unsigned char * p = reinterpret_cast<const unsigned char *>
(sqlite3_column_text(stmt, 0));
const char * finaltext = (const char *)p;
AddStringList(hList,_T(finaltext));
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
I came across some code http://zetcode.com/gui/winapi/advancedcontrols/ on creating a list box and populating it using a struct
: 我在创建列表框并使用struct
填充它时遇到了一些代码http://zetcode.com/gui/winapi/advancedcontrols/ :
typedef struct {
wchar_t name[30];
wchar_t job[20];
int age;
} Friends;
Friends friends[] = {
{L"Lucy", L"waitress", 18},
{L"Thomas", L"programmer", 25},
{L"George", L"police officer", 26},
{L"Michael", L"producer", 38},
{L"Jane", L"steward", 28},
};
Then using it as below: 然后按如下方式使用它:
case WM_CREATE:
hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD
| WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd,
(HMENU) IDC_LIST, NULL, NULL);
for (int i = 0; i < ARRAYSIZE(friends); i++) {
SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name);
}
break;
Since I could not understand the ARRAYSIZE
used above. 由于我无法理解上面使用的ARRAYSIZE
。 I substituted it with an int
5 as below: 我将其替换为int
5,如下所示:
case WM_CREATE:
hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD
| WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd,
(HMENU) IDC_LIST, NULL, NULL);
for (int i = 0; i < 5; i++) {
SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name);
}
break;
Okay now that said and done. 好了,现在说完了。
How do I create a
struct
similar to the one above of friends and populate it with values from my sqlite3 table using thefor
loop ? 如何创建类似于上述好友struct
,并使用for
循环用来自sqlite3表中的值填充该struct
?
I am still a novice in c/c++. 我仍然是C / C ++的新手。
Since I could not uderstand the "ARRAYSIZE" used above I substituted it with an int 5 as below 由于我无法理解上面使用的“ ARRAYSIZE”,因此将其替换为如下的int 5
It's most likely a macro, which can defined as: 它最有可能是一个宏,它可以定义为:
#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
This works fine for arrays that are defined in the same function where the macro is used. 对于使用宏的同一函数中定义的数组,此方法效果很好。 It won't work when the array is passed to a function. 当数组传递给函数时,它将不起作用。 In a function that get an array as an argument, sizeof(array)
will evaluate to the size of a pointer, not the size of the array. 在以数组作为参数的函数中, sizeof(array)
计算结果将是指针的大小,而不是数组的大小。
Eg 例如
void foo(int array[])
{
int s = ARRAYSIZE(array); // Won't work here.
}
void foo()
{
int array[] = {1, 3, 20};
int s = ARRAYSIZE(array); // Will work here.
}
If you have the option, you should use std::vector
. 如果可以的话,应该使用std::vector
。 That will make coding a lot easier in the long run. 从长远来看,这将使编码变得容易得多。
In C++11, you can define friends
as: 在C ++ 11中,您可以将friends
定义为:
std::vector<Friends> friends = {
{L"Lucy", L"waitress", 18},
{L"Thomas", L"programmer", 25},
{L"George", L"police officer", 26},
{L"Michael", L"producer", 38},
{L"Jane", L"steward", 28},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.