Defining c style type using typedef?

Discussion about recent product features & solutions!
13 posts • Page 2 of 2
mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Defining c style type using typedef?

Post by mkoller »

I can't see why dynlen() would deliver a wrong value. It delivers the number of items in the list, which is 100 in my code above.
I would be interested to know what exactly did not work in your case.

RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

Re: Defining c style type using typedef?

Post by RudiKreiner »

Of course if you just declare your dynamic variable like this:
dyn_anytype MsgList;
then check how big it is like this
int i = dynlen(MsgList);
you will get size 0 because virgin dynamic objects will always have size zero.
A dynamic object will automatically resized when you write to any index of it.
For example,
MsgList[55] = "hello";
int i = dynlen(MsgList);

will result in i = 55

Gertjan van Schijndel
Posts:634
Joined: Mon Aug 02, 2010 10:37 am

Re: Defining c style type using typedef?

Post by Gertjan van Schijndel »

Rudi Kreiner wrote:
dyn_anytype MsgList;
MsgList[55] = "hello";
int i = dynlen(MsgList);

will result in i = 55
In this case the length of 55 is correct, because the array needs to be enlarged to 55 items to be able to store the value at index 55. Also you can retrieving the values for the indexes 1-55 will not result in an index out of range error.
In case you want to store values at random indexes, you could better use a mapping. With a mapping mappinglen (or sizeof) will return 1 in this case.

13 posts • Page 2 of 2