Reputation: 41
I am testing sqlite3
with C
and I decided to make a simple program that takes a username and password from input in a function and passes it to be inserted into a sqlite3
database table. Problem I have is that whenever I mention the function I wrote anywhere in my code I get an error like this for each mention:
error: expected identifier or ‘(’ before ‘register’
void *register(const char *u,const char *p)
Here is my code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sqlite3.h>
void register(const char *u,const char *p)
{
printf("Enter your desired username: \n");
scanf("%s", u);
printf("Enter your desired password: \n");
scanf("%s", p);
}
int main()
{
const char new_user[50];
const char new_pass[50];
sqlite3 *db;
sqlite3_stmt *stmt;
int rc = sqlite3_open("test.db", &db);
if(rc != SQLITE_OK)
{
fprintf(stderr, "Problem opening Database: %s\n", sqlite3_errmsg(db));
return 0;
}
char *sql = "INSERT INTO Users VALUES(?, ?, '0335804828', 'Strada userului nr 9', NULL, 0);";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, "blala", -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, "blabla", -1, SQLITE_STATIC);
}
else
{
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
}
return 0;
}
As you can see code is not yet finished but it does not compile because of the mentioned error. I've searched for similar answers but nothing worked in my case. I've tried changing the function and its types but nothing works.
Upvotes: 0
Views: 50
Reputation: 68023
void register(const char *u,const char *p) { printf("Enter your desired username: \n"); scanf("%s", u); printf("Enter your desired password: \n"); scanf("%s", p); }
Two issues. You use register
as a function name, but it is the C keyword which cannot be used.
When you change the name your function parameters are wrong. const char *u
means that bytes referenced by 'u' are const
and cannot be changed.
void foo(char *u,char *p);
or if the pointers do not change
void foo(char * const u,char * const p);
Upvotes: 1
Reputation: 409472
register
is a keyword in C. You can't use keywords for names.
Upvotes: 0