C++中字符串指针的初始化问题
程序运行报错“段错误”
程序源代码粘贴如下:
14 #include "sqlite3.h"
15 void teaform::on_okButtonClicked()
16 {
17 sqlite3* db;
18 char *num,*author,*storage;
19 int rc,nRow,nCol;
20 char **dbResult;
21 char *sql,*errmsg=NULL,*str;
22
23 QString nameText=nameEdit->text();
24 QString resultQString,errMsgQString;
25
26 //open the databse:
27 rc=sqlite3_open("library1.db",&db);
28
29 //open failed:
30 if(rc)
31 {
32 errMsgQString.sprintf("cannot open database: %s\n",sqlite3_errmsg(db));
33 resultEdit->setText(errMsgQString);
34 sqlite3_close(db);
35 }
36
37 //open successed, then do querying:
38 else{
39 strcpy(str,nameText.ascii());
40 sprintf(sql,"select * from book where BookName='%s';",str);
41
42 rc=sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&errmsg);
43
44 //get table failed:
45 if(rc!=SQLITE_OK)
46 {
47 resultQString.sprintf("cannot open database: %s\n",errmsg);
48 resultEdit->setText(resultQString);
49 sqlite3_free_table(dbResult);
50 sqlite3_close(db);
51 }
52
53 //get table successfully:
54 else
55 {
56 int j=4;
57 while(j!=8)
58 {
59 switch(j)
60 {
61 case 4: strcpy(num,dbResult[j]);
62 case 5:
63 case 6: strcpy(author,dbResult[j]);
64 case 7: strcpy(storage,dbResult[j]);
65 default: break;
66 }
67 j++;
68 }
69
70 resultQString.sprintf("%s,%s,%s\n",num,author,storage);
71 resultEdit->setText(resultQString);
72 sqlite3_free_table(dbResult);
73 sqlite3_close(db);
74 }
75 }
76 }
make时有警告信息:
.ui/../teaform.ui.h: In member function ‘virtual void teaform::on_okButtonClicked()’:
.ui/../teaform.ui.h:21: warning: ‘str’ may be used uninitialized in this function
.ui/../teaform.ui.h:21: warning: ‘sql’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘num’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘author’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘storage’ may be used uninitialized in this function
大概知道是定义的指针变量方面造成了“段错误”,因此对指针做了初始化,修改后的代码如下:
13 #include "sqlite3.h"
14 #include <cstdlib>
15 using namespace std;
16 void teaform::on_okButtonClicked()
17 {
18 sqlite3* db;
19 char *num=(char*)malloc(10);
20 char *author=(char*)malloc(30);
21 char *storage=(char*)malloc(5);
22 int rc,nRow,nCol;
23 char *sql=(char*)malloc(100);
24 char *str=(char*)malloc(20);
25
26 QString nameText=nameEdit->text();
27 QString resultQString,errMsgQString;
28
29 //open the databse:
30 rc=sqlite3_open("library1.db",&db);
31
32 //open failed:
33 if(rc)
34 {
35 errMsgQString.sprintf("cannot open database: %s\n",sqlite3_errmsg(db));
36 resultEdit->setText(errMsgQString);
37 sqlite3_close(db);
38 }
39
40 //open successed, then do querying:
41 else{
42 strcpy(str,nameText.ascii());
43 sprintf(sql,"select * from book where BookName='%s';",str);
44
45 char **dbResult;
46 char *errmsg=(char*)malloc(100);
47 rc=sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&errmsg);
48
49 //get table failed:
50 if(rc!=SQLITE_OK)
51 {
52 resultQString.sprintf("cannot open database: %s\n",errmsg);
53 resultEdit->setText(resultQString);
54 sqlite3_free_table(dbResult);
55 sqlite3_close(db);
56 }
57
58 //get table successfully:
59 else
60 {
61 int j=4;
62 while(j!=8)
63 {
64 switch(j)
65 {
66 case 4: strcpy(num,dbResult[j]);
67 case 5:
68 case 6: strcpy(author,dbResult[j]);
69 case 7: strcpy(storage,dbResult[j]);
70 default: break;
71 }
72 j++;
73 }
74
75 resultQString.sprintf("%s,%s,%s\n",num,author,storage);
76 resultEdit->setText(resultQString);
77 sqlite3_free_table(dbResult);
78 sqlite3_close(db);
79 }
80 free(errmsg);
81 }
82 free(num);
83 free(author);
84 free(storage);
85 free(sql);
86 free(str);
87 }
make时是没有警告信息了,但是运行时仍然报“段错误”
该怎么修改呢,大家帮忙看看。。。