LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gdbm with C - compilation problem. (https://www.linuxquestions.org/questions/programming-9/gdbm-with-c-compilation-problem-4175426236/)

vectrum 09-08-2012 07:54 AM

gdbm with C - compilation problem.
 
Please let me know why the following code doesn't compile when I try to compile on Dev-C++, it emits the following error msgs (first few err msgs given) but it is compiled when all the argv[]s are placed under double quote like "argv[]".
Another code, which is given after it, is compiled without any problem. Both codes taken from
the web.

:17: warning: unused variable `dbf'
18: warning: unused variable `key'
:18: warning: unused variable `data'
:25: warning: type defaults to `int' in declaration of `dbf'
:25: error: `argv' undeclared here (not in a function)
:25: warning: initialization makes integer from pointer without a cast
....
....

#include<stdio.h>
#include<gdbm.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char *argv[])
{
GDBM_FILE dbf;
datum key, data;

if (argc < 3)
fprintf (stderr, "Usage: dbmexample \n\n");
exit (1);
}

dbf = gdbm_open(argv[1], 0, GDBM_READER, 0666, 0);
if (!dbf)
{
fprintf (stderr, "File %s either doesn't exist or is not a gdbm file.\n", argv[1]);
exit (2);
}

key.dsize = strlen(argv[2]) + 1;

data = gdbm_fetch(dbf, key);

if (data.dsize > 0) {
printf ("%s\n", data.dptr);
free (data.dptr);
} else {
printf ("Key %s not found.\n", argv[2]);
}
gdbm_close (dbf);
return 0;
}
----------------------------------------
This code doesn't give any problem at all.

#include <stdio.h>
#include <gdbm.h>

int
main(void)
{
GDBM_FILE dbf;
datum key = { "testkey", 7 }; /* key, length */
datum value = { "testvalue", 9 }; /* value, length */

printf ("Storing key-value pair... ");
dbf = gdbm_open ("test", 0, GDBM_NEWDB, 0644, 0);
gdbm_store (dbf, key, value, GDBM_INSERT);
gdbm_close (dbf);
printf ("done.\n");
return 0;
}

lyle_s 09-08-2012 10:48 AM

I think you're missing a bracket:

Code:

datum key, data;
 
 if (argc < 3) {
 fprintf (stderr, "Usage: dbmexample \n\n");
 exit (1);
 }
 
 dbf = gdbm_open(argv[1], 0, GDBM_READER, 0666, 0);

The compiler thinks you've ended the main function with the "}" after "exit (1)", so it doesn't think argv exists in your call to gdbm_open.

Lyle.

vectrum 09-08-2012 10:59 AM

Thank you very much lyle for your help
 
lyle_s:hattip:
Thank you very much lyle_s for your help.
Oh God, my problem is solved.
Thanks lyle_s
:newbie:
that was really silly of me.
Bye...


All times are GMT -5. The time now is 04:01 PM.