0

我正在尝试使 C 程序正常工作,但我很生气。这是我为查找错误而简化的代码:

#include <stdio.h>
#include <unistd.h>
#include <sqlite3.h>

int main(){
    sqlite3 *conn;
    sqlite3_stmt *res;
    const char *tail, *sqlresult;
    sqlite3_open("cubecat", &conn);
    char buffer,query;
    int id;

    id= 1;
    buffer = 'a';
    if(buffer == 'a') snprintf(&query,100,"SELECT start FROM payloads WHERE id=%d", id);

    printf("%s",&query);
    int error = sqlite3_prepare_v2(conn, &query, 100, &res, &tail);
    printf("%d",error);
}

错误正是在“sqlite_prepare_v2”函数上,因为如果我评论那行,就没有分段错误。

先感谢您!

4

1 回答 1

1
char query;
snprintf(&query,100,"SELECT start FROM payloads WHERE id=%d", id);

这就是问题所在。query只为一个字符保留内存。的第二个参数snprintf()指定大小是有原因的。这段代码应该这样修改:

char query[100];
snprintf(query, sizeof(query), "SELECT start FROM payloads WHERE id=%d", id);
于 2013-02-16T17:49:03.940 回答