From: Thimble Smith Date: March 22 1999 6:07am Subject: Re: Why the error here? List-Archive: http://lists.mysql.com/mysql/757 Message-Id: <19990321230711.R25614@desert.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Thomas, > char *db = "db"; > > /* ... */ > > printf("Enter a name for your new database\n\n"); scanf("%s", &db); This is a Frequently Asked Question, but for comp.lang.c, not for this MySQL list. I highly recommend (read: you must do this; must) that you read the entire comp.lang.c Frequently Asked Questions list. Don't just skim it. Spend a day with it, reading every question over and over until you understand it. You will not be sorry you did it; if you don't understand every question on the FAQ, you WILL make one of the mistakes (like the one you're making now), and you WON'T know that you've made it or why what you did is wrong. The C FAQ is one of the best things for beginning C programmers to read. Don't be intimidated by it; print it out, sit down with it and a GOOD C book, and go through it. You've actually got several problems in your code. For one, your variable db is pointing to a STRING CONSTANT, which is NOT the same as a character array. It's pointing to read-only memory, which means that you can't assign to it. Second, you're passing scanf the address of the pointer, not the address of the character array. So, you're trying to store characters over top of the pointer. Not good. If you look at question 7.1 of the C FAQ, you'll see an example of how to do what you're trying. But PLEASE, PLEASE don't just read that example, cut-n-paste, and then go on making more mistakes like this. You MUST read the FAQ; you can't just type things in C and expect them to work! You have to know what you're doing! If you cut and paste and plow along, you will be writing programs that are full of bugs, and you might not know it. You might think they're working fine, and they're scribbling all over memory (if you're on a lousy non-protected OS) or they're threatening to dump core at the slightest provokation. Do yourself a favor and don't write another line of code in your real app until you've read through the entire FAQ. Then start your app all over again. Tim P.S. After you've read and understood the FAQ, write code for a week or so. Then spend another day reading the FAQ again. You should read the FAQ every week for at least two months while you're learning C. At least once a week! On Sun, Mar 21, 1999 at 10:20:52PM -0500, Thomas R. Bedell wrote: > Hello, > > I am trying to use: > > mysql_create_db in a C program. It compiles and links OK. But > when you execute the program, I get the following error. > > #include "mysql.h" > #include "mysql.h" > #include > #include > > MYSQL mysql; > MYSQL_RES *res; > MYSQL_ROW row; > char *db = "db"; > > void exiterr(int exitcode) > { > fprintf( stderr, "%s\n", mysql_error(&mysql) ); exit( exitcode ); > } > > main() > { > char db; > printf("Enter a name for your new database\n\n"); scanf("%s", &db); > if (!(mysql_connect(&mysql,"localhost","root",""))) exiterr(1); > if (mysql_create_db(&mysql, &db)) > exiterr(2); > > mysql_free_result(res); > mysql_close(&mysql); > } > > When I run my code here is what I get: > [root@oscar mysql_c]# ./connect > Enter a name for your new database > > homes > Segmentation fault > BUT.... The database is added to mysql. I can connect to it, > and view the "empty set". Why the segmentation fault???