Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pointers to Types with strings howto?
#1
Let's say I have a type with a variable length string in it.
I then want to send a pointer of it to a sub and manipulate the
string there:
[syntax="qbasic"]TYPE mttype
foo as integer
bar as string
END TYPE

DIM mytype as mttype
DIM mtptr as mttype ptr
mtptr=@mytype
mysub mtptr

sub mysub (thedata as mttype ptr)
print *thedata->bar, mid$(*thedata->bar,2,1)
*thedata->bar=*thedata->bar+"something"
*thedata->bar=right$(*thedata->bar,10)
end sub
[/syntax]

What I need to know is how to change the data in mytype.foo
through a pointer, and by change I mean add/remove/change
characters. I also need to know if I have put the '*'s and '@'s
in the right place, and if it's the right characters to use.
Please mind the fact that I have no copy of fB or a cumputer I can
run it on, so I can't check any examples...
/post]
Reply
#2
For changing properties of stuctures, you use pointer dereferencing:

[syntax="qbasic"]dim mytypeptr as mytype ptr ' makes mytypeptr a null pointer
mytypeptr = callocate( len(mytype) ) ' points it to some memory
mytypeptr->foo = 42 ' now you can do crap.[/syntax]

You could also do *mytype.foo (I think), but above is the way to do it, as I understand the case.

Stuff I'm assuming you know:
myptr = @myvar
myvar = *myptr

Otherstuff:
myptr->foo = *myptr.foo

Or at least that's what I've been doing and it's working. I don't like standard pointer notation, it's confusing, and has too many symbols. I wish they'd stick to one symbol like "@" or "*" and use it for everything poitner-related.
Reply
#3
So I can do this?:

[syntax="qbasic"]Type footype
bar as string
end type

dim pfoo as footype ptr
pfoo=callocate(len(footype))
do
pfoo->bar=pfoo->bar+chr$(rnd*255)
pfoo->bar=left$(pfoo->bar,10)
loop while inkey$=""[/syntax]

And get the result that pfoo is a pointer to a foo type
and the loop edits the bar string in said type?
/post]
Reply
#4
I just tested that with the 0.12 and 0.13 it works, I didn't test 0.14, but I assume it would work too.

On a side note, it's not a good idea to handle strings like that as the string descriptor is technically invalid. I'm guessing from my test result that freebasic notices this and allocates the string properly, validating the descriptor. Still, this is bad programming practice to "assume" the compiler is going to do something. It would be nice if v3cz0r would confirm that this is indeed how things are intended to and will continue to work in the future.
Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Reply
#5
That might be something good to note in the wiki if you find it out.

One thing the compiler chokes on is a hash + linked-list example I'm trying to learn the language with, but instead of not working, it simply doesn't store structure pointers in dynamic arrays (via callocate and ptrs) (it'll do it, but the instant I drop the replace the variable containing the pointer, the memory deallocates).
Reply
#6
I have a nice high-speed hand-coded, free from artificial colours, not tested on animals set of hashing routines I wrote in fb if people want a copy.

In it I carefully handle strings manually so there are no issues with "does this work". ( I convert strings to length + byte ptr. )

It's setup so that you can have an unlimited size hash-table and as many as you want, so long as you have the memory. Also, this doesn't use linked lists so it's a fair bit faster and more simplified.

I'm not at home, but feel free to fire me an email at eric@cowles.ca (should be working by midnight).
Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)