c++ - How do I declare and use an array of struct in a for loop? -
i working on native winapi application using cpp , sqlite http://github.com/jacksiro/vsongbook-cpp using falcon c++ ide. 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 database insert this:
void inserttodatabase() { int db_qry; char *error; sqlite3 *db; const char *sqlinsert = "insert my_friends(friend_name, friend_job, friend_age) " "values('ngunjiri james', 'teacher', 25);" "insert my_friends(friend_name, friend_job, friend_age) " "values('wafula shem', 'capernter', 30);" "insert 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 have simple listbox in window populate values sqlite table this.
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 method in wm_create in winmain , works void addfriendlistbox(const hwnd hlist) { sqlite3 * db = null; sqlite3_stmt * stmt; int i, db_qry; const char * tail; const char * sqlselect = "select * 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; < 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 code http://zetcode.com/gui/winapi/advancedcontrols/ on creating list box , populating using struct
:
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 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); (int = 0; < arraysize(friends); i++) { sendmessagew(hwndlist, lb_addstring, 0, (lparam) friends[i].name); } break;
since not understand arraysize
used above. substituted int
5 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); (int = 0; < 5; i++) { sendmessagew(hwndlist, lb_addstring, 0, (lparam) friends[i].name); } break;
okay said , done.
how create
struct
similar 1 above of friends , populate values sqlite3 table usingfor
loop?
i still novice in c/c++.
since not uderstand "arraysize" used above substituted int 5 below
it's macro, can defined as:
#define arraysize(array) (sizeof(array)/sizeof(array[0]))
this works fine arrays defined in same function macro used. won't work when array passed function. in function array argument, sizeof(array)
evaluate size of pointer, not size of array.
e.g.
void foo(int array[]) { int s = arraysize(array); // won't work here. } void foo() { int array[] = {1, 3, 20}; int s = arraysize(array); // work here. }
if have option, should use std::vector
. make coding lot easier in long run.
in c++11, can define friends
as:
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}, };
Comments
Post a Comment