Home » Developer & Programmer » Precompilers, OCI & OCCI » .arr and .len
.arr and .len [message #437069] Wed, 30 December 2009 21:02 Go to next message
myura
Messages: 66
Registered: July 2007
Location: Malaysia
Member

Good day all,

I am very new in pro*c. And currently still learning and try to understand the pro*c syntax.

One of that i don't really get is .arr and .len .
I've search from the internet and seems like no details explanation about this. For example, one of the program may looks like this:

#define NULLSTR(x) x.arr[x.len] = '\0';


and used in the body like this:

NULLSTR(ID_NO)


where id_no is int type.

Can somebody explain what does the .arr and .len means?
Re: .arr and .len [message #437074 is a reply to message #437069] Wed, 30 December 2009 22:26 Go to previous messageGo to next message
calluru
Messages: 17
Registered: July 2009
Location: Austin, TX, USA
Junior Member
When you declare a variable as VARCHAR in your Pro*C code between SQL DECLARE section, the Pro*C pre-compiler translates them into C structures. Within the structure the VARCHAR data will be stored in a string array called arr and the length of the string is stored in the member variable called len.

And whenever you fetch string values from database into one of these VARCHAR variables you would have to append the string with a null terminator. I think this is a C thing. Try printing these string values with and without the null terminator you will see it what I mean.

For more information look up for handling string in Pro*C.

-- Balaji
Re: .arr and .len [message #437113 is a reply to message #437069] Thu, 31 December 2009 02:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I am very new in pro*c. And currently still learning and try to understand the pro*c syntax.

A good reading to start using Pro*C: Pro*C/C++ Programmer's Guide

Quote:
and used in the body like this:

NULLSTR(ID_NO)

where id_no is int type.

If what you say is real this should produce a compilation error.
Note that NULLSTR is a macro define in your program and is not part of Pro*C. Take care using it or you might overwrite your stack.

Regards
Michel

[Updated on: Thu, 31 December 2009 02:22]

Report message to a moderator

Re: .arr and .len [message #437146 is a reply to message #437113] Thu, 31 December 2009 04:03 Go to previous messageGo to next message
myura
Messages: 66
Registered: July 2007
Location: Malaysia
Member

Michel: Yes, it produced a compilation error.
It's true, the NULLSTR is defined in my program and not part of pro*c.

I just want to see the output and understand what does NULLSTR do and modified my program into like this:
#include <stdio.h>
#include <string.h>

#define NULLSTR(x) x.arr[x.len] = '\0'

int main()
{
 varchar test[10];
 strcpy((char *)test.arr,"test");

 printf("Testing  %s \n",NULLSTR(test)); 

}



Successfully compiled. But then it print out : "Segmentation fault (core dump)".

Is it something to do with the memory?
Re: .arr and .len [message #437149 is a reply to message #437146] Thu, 31 December 2009 04:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
In your code you didn't set x.len so it is not surprising you get a core dump.
This is one of the reason why I said: "Take care using it or you might overwrite your stack".

Regards
Michel
Re: .arr and .len [message #437320 is a reply to message #437069] Sun, 03 January 2010 21:34 Go to previous message
myura
Messages: 66
Registered: July 2007
Location: Malaysia
Member

Ok, got it. Thanks a lot. Smile
Previous Topic: Any impact on pro*c code when upgrading to 11g from 9i
Next Topic: Oracle OCI: Problem in Query with Date field
Goto Forum:
  


Current Time: Fri Mar 29 02:08:12 CDT 2024