April 26, 2024, 07:41:50 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Why are only three characters shown in string created for four?

Started by AdrianFox, January 17, 2014, 10:37:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AdrianFox

Just intrigued why this might be happening.  (Of course I have simply 'solved' the problem by increasing the istring to 5 but don't understand why the problem occurred.)  There is something in the back of my mind about some strings being zero based... ????

type seeddataentry
DEF YearNum[4] as istring
def Seedname[50] as istring
........etc etc.
endtype


The YearNum istring simply contains the year obtained from date$("yyyy")
but it is saved in the file as only 3 characters not four.

I  read the string from the relevant editbox with getcontroltext and then save it to a random access file in the conventional way with PUT. 
???
Adrian Fox

Sam

Because one of the spaces allocated to the string is for the null terminating character. If you want an n length string, you must allocate n + 1 characters.

LarryMc

Adrian

Tennisbum is absolutely correct.

When you define a STRING variable IWB allows for 255 characters + the null terminator.
When  you define an ISTRING variable you always have to account for the space for the null character.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

AdrianFox

Thanks guys, I now see that the null terminator is normal in any string to mark the end of the string. 

What misled me was the example in the Help file which says 'Define a string of EXACTLY 30 characters
DEF name[30]:istring

Though I see now the existence of the null character is fairly clearly stated in the first two paragraphs of the Help file on Strings.  For some reason I thought ISTRING was different.

;)

Adrian Fox

LarryMc

Quote from: AdrianFox on January 18, 2014, 02:01:37 AM
What misled me was the example in the Help file which says 'Define a string of EXACTLY 30 characters
DEF name[30]:istring
will read
QuoteDEF name[31]:istring
in next update

Just wanted to mention the '?' wildcard mentioned above.

The "?" means I don't care about the single character located at that location.

For older users you can try this with FIF:
if you wanted to search for just eba, iba, and cba files you would set the filter to '*.?cb'


The * and ? are carry overs from way back in the DOS days with the DIR command.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Hmm
DEF i$[30] :ISTRING  ' define istring array
def i,x as INT
string s
x=65
for i = 1 to 30

s=chr$(x)
i$[i]=s
print i$[i]
x++
next i

WAITCON


If i understand corectlly Istring type can hold more than 255 chars
and i don't see connection between LEN in this case because i$[30] is a (i)string array
and each element can hold more than 255 chars ..right?
Of course if is each elemnt filled with istring not with string like i do in my example.
Infact each string on windows is array of characters.

LarryMc

okay, here we go

STRINGS

STRING adefines a string 255 characters long full of 255 NULLs

LEN(a) returns 0 because it counts the number of characters before it encounters a NULL

a="QQ"
LEN(a)
now returns 2
and so on to a max of 254 characters because the 255th character has to be a NULL

SIZEOF(a)
Always returns the amount of memory reserved for the variable, regardless of type or content.  For a single STRING type this will always be 255

------------------------------------------------------------------
STRING arrays

STRING b[10]defines an array of 10 strings with each string 255 characters long full of 255 NULLs

LEN(b) generates an error

b[5]="qqq"
LEN(b[5])
now returns 3
and so on to a max of 254 characters (because the 255th character has to be a NULL) for the 6th element of the array

SIZEOF(b[5])will always return 255 regardless of the contents of the element

SIZEOF(b)in this case will return 255 x 10 = 2550 regardless of arrays contents

==========================================
ISTRINGS

ISTRING a[5]defines an istring 5 characters long full of 5 NULLs

LEN(a) returns 0 because it counts the number of characters before it encounters a NULL

a="QQ"
LEN(a)
now returns 2
and so on to a max of 4 characters because the 5th character has to be a NULL

SIZEOF(a)
returns the amount of memory reserved for the variable, regardless of type or content.  In this case it will be 5

------------------------------------------------------------------
ISTRING arrays

ISTRING b[5,10]defines an array of 10 strings with each string 5 characters long full of 5 NULLs

LEN(b) generates an error

b[0,5]="abc"
LEN(b[0,5])
now returns 3
and so on to a max of 4 characters (because the 5th character has to be a NULL) for the 6th element of the array

PRINT b[0,5] prints "abc"

PRINT b[1,5] prints "bc"

PRINT b[2,5] prints "c"

PRINT b[15] prints "abc"

PRINT b[16] prints "bc"

PRINT b[17] prints "c"

SIZEOF(b[0,5])will always return 5 regardless of the contents of the element

SIZEOF(b)in this case will return 5 x 10 = 50 regardless of arrays contents

***************************
Note The indexing into memory of STRING arrays is handled by the compiler for you.  With ISTRING arrays you have to help it out which is why a single dimension ISTRING array requires 2 parameters.  If you had a two dimensional ISTRING array it would need 3 parameters.

Hope this helps explain the difference between the two types.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library