... and the last one for today: structures with []

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

... and the last one for today: structures with []

Post by blueznl »

this might turn out to be a bummer :-(

a wsadata structure has a fixed length, 398 bytes, to be exact

if i would define it as:

Structure wsadata
wversion.w
whighversion.w
description.s
systemstatus.s
imaxsockets.b
imaxudpfg.b
vendorinfo.l
Endstructure

then i would have no control over the length of the structure, unless i would fill the strings with a number of bytes... of, are these string elements of the structure actually pointers to the strings? but why are they then defined with a specific length in the api?

when i look at one of the samples, it conflicts with the help doc, and i see the following kind of statement:

Structure xyz
abc.s[12]
Endstructure

what does the [12] mean?

i'm totally lost now, some help would be seriously appreciated! gonna hit the sheets now my brain has blown a fuse... thanks in advance...
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

I would believe that means that there can be an array, "abc", of 13 strings (each string restricted to 64000 chars) in the structure "xyz".. Someone correct med if I'm wrong..

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

LarsG wrote:I would believe that means that there can be an array, "abc", of 13 strings (each string restricted to 64000 chars) in the structure "xyz".. Someone correct med if I'm wrong..

-Lars
Actually it's an array of 12 strings indexed 0-11. ;)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

viewtopic.php?t=7134

Square brackets mean a static array that cannot be re-Dim'd. Also IIRC string variables are really pointers to the string itself.

Code: Select all

String.s = "hello"
Debug Int(String)
Debug PeekS(Int(String))
--Kale

Image
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

euh, but gentlemen, if the above is true, how do i do the following?

let's say a system struct contains 40 bytes, of which the first 4 are an int and the rest is a string of 32 bytes... how do i declare such a beast in pure with the structure command?

in another mail on the forum viewtopic.php?t=6214&highlight=wsastartup i found the following code:

Structure WSADataType
wVersion.w
wHighVersion.w
szDescription.s[#WSA_DescriptionSize]
szSystemStatus.s[#WSA_SysStatusSize]
iMaxSockets.w
iMaxUdpDg.w
lpVendorInfo.l
EndStructure

... but szDescription.s would then be a pointer... shouldn't it be a string with fixed length and how to do it?

as you can tell, i am really lost...
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Try it this way:

Code: Select all

Structure WSADataType
  wVersion.w
  wHighVersion.w
  StructureUnion
    szDescription.s
    szDescription_Chars.b[#WSA_DescriptionSize]
  EndStructureUnion
  StructureUnion
    szSystemStatus.s
    szSystemStatus_Chars.b[#WSA_SysStatusSize]
  EndStructureUnion
  iMaxSockets.w
  iMaxUdpDg.w
  lpVendorInfo.l
EndStructure
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

an array of bytes... hmmm... yeah, that might work...

Structure xyz
StructureUnion
a.s
b.b[10]
EndStructureUnion
EndStructure

would would the STRING a.s now be at the same location as b.b(1) or is it the pointer TO the string?

(here's the original c structure, i forgot to add it)

WORD wVersion
WORD wHighVersion
char szDescription_PAL_Undefined [WSADESCRIPTION_LEN+1]
char szSystemStatus_PAL_Undefined [WSASYS_STATUS_LEN+1]
unsigned short iMaxSockets_PAL_Undefined
unsigned short iMaxUdpDg_PAL_Undefined
char FAR * lpVendorInfo_PAL_Undefined
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Sorry I was wrong ... too fast writing ... too less thinking :-(

Do it like this:

Code: Select all

Structure WSADataType
  wVersion.w
  wHighVersion.w
  szDescription.b[#WSA_DescriptionSize]
  szSystemStatus.b[#WSA_SysStatusSize]
  iMaxSockets.w
  iMaxUdpDg.w
  lpVendorInfo.l
EndStructure
To get the string from the structure:

Code: Select all

aString = PeekS(@struct\szDescription)
To write the string to the structure:

Code: Select all

PokeS(@struct\szDescription, aString)
Attention: The length of the string (including NullByte at end of string) must not be longer than #WSA_DescriptionSize.

cu helpy
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

yeah, i came to the same conclusion...

pity that pure doesn't have a fixed length string type, that would make access to such structs easier, something like:

Structure xyz
a.c[5]
Endstructure

... and for all other purposes consider it a string, then you could do direct assignments, such as:

a="123"

... which is quite a bit more elegant than using Poke (brrrr)

is there another command / syntax that i overlooked?

i can think up some other alternative syntaxes but i guess fred won't be implementing them :-/
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

A String is always a pointer in PureBasic:

Code: Select all

Structure Test
  String.s
EndStructure

Debug SizeOf(Test)
Structure is 4 bytes, a pointer to the real String. So to simulate a fixed string,
that is actually inside the Structure, you can only use an array of bytes.

Timo
quidquid Latine dictum sit altum videtur
Post Reply