Re: VARCHAR variable problem

From: Thomas J Kyte <tkyte_at_us.oracle.com>
Date: 1996/02/16
Message-ID: <4g296d$n8_at_inet-nntp-gw-1.us.oracle.com>#1/1


There is one definite problem and the chance of another problem with your code. First, the definite problem is:

> printf("%s\n", countyName);

countryName is a Structure. You need to

printf( "%s\n", countryName.arr )

                           ^^^^

Surprised that it didn't segfault. It would definitely lead to unpredicable printf behaviour.

The other potential problem is you should check to make sure the countryName.len < 16. If the database field is declared as a varchar2(16) or bigger, then you stand a chance of doing an overwrite of the countryName.arr element of the varchar structure. Always declare your varchar host variables to be at least one bigger then the database field if you plan on null terminating them.

You can blow off the null terminating and just try the following printf as well:

printf( "%*.*s\n", countryName.len, countryName.len, countryName.arr );

Al Holloway <alh_at_tiger.avana.net> wrote:

>Salutations to all,
 

>I'm having a serious problem with the VARCAR variable (countyName)
>declared in the function listed below. Nothing is printed when
>this function is called as is. However, if I remove the line
>that null-terminates countyName, then there's output from the
>first three printf statements and nothing from the last one.
 

>The declaration and usage of variable countyName is identical to
>the variable emp_name of program SAMPLE1.PC listed on page 4-2
>in Pro*C Supplement To The Oracle Precompilers Guide.

>void printTitles(int code)
>{
> char tdate[10];
 

> EXEC SQL BEGIN DECLARE SECTION;
 
> VARCHAR countyName[16];
> int cCode = code;
 

> EXEC SQL END DECLARE SECTION;
 
> EXEC SQL SELECT county_name INTO :countyName FROM system.county_mstr
> WHERE county_id = :cCode;
 

> getTodaysDate( tdate );
> printf("\t\t\t\t\t\t\t\t%s\n",tdate);
> printf("\n\n\n\t\t\tDISEASE SUMMARY\n");
> printf("\n\nCOUNTY: ");
> countyName.arr[countyName.len] = '\0';
> printf("%s\n", countyName);
 

> return;
>}

>Oracle7 Server 7.1.6, Pro*C 2.0.6 and Solars 2.4 is the environment
>I'm programming in.
 

>All responses are appreciated.
 

>Thanks,
 

>Al Holloway
>abh_at_ph.dhr.state.ga.us
>alh_at_tiger.avana.net

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government



opinions and statements are mine and do not necessarily reflect the opinions of Oracle Corporation. Received on Fri Feb 16 1996 - 00:00:00 CET

Original text of this message