structures in ESQL/C
Posted in 1999
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
I am having a problem with structures inside a header file and using them in an .ec file. I took this example straight from the documentation and it is not compiling. I am trying to use a structure that is defined in a header file, "i.h" and I am getting two errors, one where the variable is defined, esqlc: "sample.ec", line 12: Warning -33014: Illegal use of record/structure 'CUST_REC'. and one where the variable is used, esqlc: "sample.ec", line 21: Error -33037: 'c_no' is not a component of record 'cust'. Here is the complete code i.h file #define LEN 15 typedef struct customer_t { int c_no; char fname[LEN]; char lname[LEN]; } CUST_REC; // end i.h sample.ec file #include <stdio.h> #define LEN 15 $include "i.h"; // tried putting this in a declare section with no success void main() { EXEC SQL BEGIN DECLARE SECTION; CUST_REC cust; // <- error on this line EXEC SQL END DECLARE SECTION; EXEC SQL PREPARE TestPrep FROM 'SELECT count(*) from store'; EXEC SQL DECLARE TestCurs CURSOR FOR TestPrep; EXEC SQL OPEN TestCurs; EXEC SQL FETCH TestCurs INTO :cust.c_no; // <- error on this line EXEC SQL CLOSE TestCurs; EXEC SQL FREE TestCurs; printf( "Number of rows in the store table are: %d\\n", cust.c_no ); } When I declare the structure above the main function it works fine. Is there some setting that I am leaving out or am I just doing something wrong. Any help will be appriciated. -- Mel Haynes Jr mailto:mhaynesjrIHATESPAM@pcisys.net ( remove IHATESPAM to respond )
Mel Haynes Jr wrote: > I am having a problem with structures inside a header file and > using them in an .ec file. I took this example straight from the > documentation and it is not compiling. I am trying to use a > structure that is defined in a header file, "i.h" and I am getting > two errors, one where the variable is defined, You're lucky that (a) I didn't spot the no spam till I'd decided to answer, and (b) that I'm using a news reader to reply just to the newsgroup and not an email system. You would not be getting an answer otherwise. > esqlc: "sample.ec", line 12: Warning -33014: Illegal use of > record/structure 'CUST_REC'. > > and one where the variable is used, > > esqlc: "sample.ec", line 21: Error -33037: 'c_no' is not a > component of record 'cust'. > > Here is the complete code > > i.h file > > #define LEN 15 > > typedef struct customer_t { > int c_no; > char fname[LEN]; > char lname[LEN]; > } CUST_REC; > > // end i.h That all looks kosher. Of course, ESQL/C won't be entirely happy because it cannot or does not analyse the #define, and therefore doesn't know how big those fields are. I've argued (unsuccessfully) that it doesn't need to know what LEN evaluates to, but I suspect my arguments were not understood. I ran out of energy before getting this issue (bug?) resolved -- we're talking about 3 years or so ago. > sample.ec file > > #include <stdio.h> > #define LEN 15 This seems redundant -- delete the LEN in sample.ec. > $include "i.h"; // tried putting this in a declare section with no success Hmmm. Are you sure? I'd expect the following to work. EXEC SQL BEGIN DECLARE SECTION; EXEC SQL INCLUDE i.h; EXEC SQL END DECLARE SECTION; I'm at home so I don't have my archive of esoterica at hand, but I know I discussed this in c.d.i maybe 5 years ago -- it might even be longer than that. Trouble is, that means it probably isn't in the Deja archives. If the solution I've suggested doesn't work, try replacing the LEN with 15 (as well as the declare section) and see whether that works. If neither works, drop me an email at the office (no antispam or I shall definitely ignore you!) and I'll post my ancient solution again. Did you check the FAQ at the IIUG archive? > void main() > { > EXEC SQL BEGIN DECLARE SECTION; > CUST_REC cust; // <- error on this line > EXEC SQL END DECLARE SECTION; > > EXEC SQL PREPARE TestPrep FROM > 'SELECT count(*) from store'; > > EXEC SQL DECLARE TestCurs CURSOR FOR TestPrep; > > EXEC SQL OPEN TestCurs; > EXEC SQL FETCH TestCurs INTO :cust.c_no; // <- error on this line > EXEC SQL CLOSE TestCurs; > EXEC SQL FREE TestCurs; > > printf( "Number of rows in the store table are: %d\\n", cust.c_no ); > } > > When I declare the structure above the main function it works fine. > Is there some setting that I am leaving out or am I just doing > something wrong. Any help will be appriciated. -- Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN #include <disclaimer.h>
You have to have the included file in a EXEC SQL BEGIN DECLARE SECTION and it must be changed from a #include to an EXEC SQL INCLUDE so the file is included by the ESQL parser not the cpp preprocessor. Last do not post with NOSPAMs in you email address if you expect to get replies. I'm one of the few around here willing to deal with them and rarely at that. Art S. Kagel Mel Haynes Jr wrote: > > I am having a problem with structures inside a header file and using them in > an .ec file. I took this example straight from the documentation and it is > not compiling. I am trying to use a structure that is defined in a header > file, "i.h" and I am getting two errors, one where the variable is defined, > > esqlc: "sample.ec", line 12: Warning -33014: Illegal use of record/structure > 'CUST_REC'. > > and one where the variable is used, > > esqlc: "sample.ec", line 21: Error -33037: 'c_no' is not a component of > record 'cust'. > > Here is the complete code > > i.h file > > #define LEN 15 > > typedef struct customer_t { > int c_no; > char fname[LEN]; > char lname[LEN]; > } CUST_REC; > > // end i.h > > sample.ec file > > #include <stdio.h> > #define LEN 15 > $include "i.h"; // tried putting this in a declare section with no success > > void main() > { > EXEC SQL BEGIN DECLARE SECTION; > CUST_REC cust; // <- error on this line > EXEC SQL END DECLARE SECTION; > > EXEC SQL PREPARE TestPrep FROM > 'SELECT count(*) from store'; > > EXEC SQL DECLARE TestCurs CURSOR FOR TestPrep; > > EXEC SQL OPEN TestCurs; > EXEC SQL FETCH TestCurs INTO :cust.c_no; // <- error on this line > EXEC SQL CLOSE TestCurs; > EXEC SQL FREE TestCurs; > > printf( "Number of rows in the store table are: %d\\n", cust.c_no ); > } > > When I declare the structure above the main function it works fine. Is there > some setting that I am leaving out or am I just doing something wrong. Any > help will be appriciated. > > -- > Mel Haynes Jr > mailto:mhaynesjrIHATESPAM@pcisys.net > ( remove IHATESPAM to respond )
Hi Mel, try someting like this, I think it works $typedef struct customer_t { int c_no; char fname[LEN]; char lname[LEN]; } CUST_REC; and $include i.h; Dirk Mel Haynes Jr schrieb: > I am having a problem with structures inside a header file and using them in > an .ec file. I took this example straight from the documentation and it is > not compiling. I am trying to use a structure that is defined in a header > file, "i.h" and I am getting two errors, one where the variable is defined, > > esqlc: "sample.ec", line 12: Warning -33014: Illegal use of record/structure > 'CUST_REC'. > > and one where the variable is used, > > esqlc: "sample.ec", line 21: Error -33037: 'c_no' is not a component of > record 'cust'. > > Here is the complete code > > i.h file > > #define LEN 15 > > typedef struct customer_t { > int c_no; > char fname[LEN]; > char lname[LEN]; > } CUST_REC; > > // end i.h > > sample.ec file > > #include <stdio.h> > #define LEN 15 > $include "i.h"; // tried putting this in a declare section with no success > > void main() > { > EXEC SQL BEGIN DECLARE SECTION; > CUST_REC cust; // <- error on this line > EXEC SQL END DECLARE SECTION; > > EXEC SQL PREPARE TestPrep FROM > 'SELECT count(*) from store'; > > EXEC SQL DECLARE TestCurs CURSOR FOR TestPrep; > > EXEC SQL OPEN TestCurs; > EXEC SQL FETCH TestCurs INTO :cust.c_no; // <- error on this line > EXEC SQL CLOSE TestCurs; > EXEC SQL FREE TestCurs; > > printf( "Number of rows in the store table are: %d\\n", cust.c_no ); > } > > When I declare the structure above the main function it works fine. Is there > some setting that I am leaving out or am I just doing something wrong. Any > help will be appriciated. > > -- > Mel Haynes Jr > mailto:mhaynesjrIHATESPAM@pcisys.net > ( remove IHATESPAM to respond )