C equivalent to Eachin
BlitzMax Forums/BlitzMax Beginners Area/C equivalent to Eachin
| ||
How would you do this in C?Local fruit:String[]=["apple","orange","banana"] For Local i:String=EachIn fruit Print i Next |
| ||
There's no equivalent of eachin. You would have to implicitly loop over an array are char pointers. untested: char* fruit[]={"apple","orange","banana"}; for (i=0;i<4;I++) { sprintf(fruit[i]); } |
| ||
int test() { char *text[]={"first","second","third"}; int i; for (i=0; i<(sizeof(text)/sizeof(int)) ; ++i) { printf("%d - %s\n",i,text[i]); } } |
| ||
Thanks both Can String[] fruit be used instead of char* fruit[]? Expanding on this, how would you mimic TLists which can contain various object types? Type alien Field name$ Field x:Int,y:Int End Type Global a:alien Global alist:TList=New TList a=New alien ; a.name="Zorb" ; alist.AddLast a a=New alien ; a.name="Al" ; alist.AddFirst a For a=EachIn alist Print a.name Next |
| ||
If you #include <string> then yes Otherwise string does not exist in C at all. C / C++ as languages are archaic at best, most of the stuff modern languages offer come in through includes. Eachin and similar things commonly through the STL and alike. |
| ||
yea. but #include <string> is c++ only, not working in C. so when use C then that char* fruit[] is same as fruit:String[] in bmax. and my C/C++ skill is so low so i cannot say anything about that TLists question. |
| ||
regular c should eat #include <string> too, given we talk about current x86 platforms (ie anything on which BM runs), not some c 99 platform. But should they not eat it, there is always #include <string.h> As for the char* fruit[] thing: That sadly is not directly comparable to BM. BM uses unicode strings which require 2 byte per character (WString), not a single one (CString). Thats important to keep in mind if you should be considering using it outside the western language world. |
| ||
oh.. i forgot unicode thing... but still i cannot use <string> or <string.h> (of course they works) but i cannot use string as variable type. |
| ||
String is not known in plain vanilla C. The include of <string.h> just gives you some basic string manipulation functions like strcpy() or strlen() but that's pretty much it. A class or type String is only valid in C++. And in plain C there is no such thing as a list type. You have to create your own lists with using pointers in your struct definitions. Something like struct mylist { struct mylist *next; struct mylist *prev; char *name; }; And of course you would have to malloc those structs and assign the pointers manually. Oh, those were the days... |
| ||
Great help all. Thanks |
| ||
Oh, those were the days... *goes all nostalgia eyed* *sniff sniff* :) |
| ||
What am I happy that the "goto - spaghetti commit suicide code - hit your project comrades for their shit pointer hack code" days are gone ... its worse enough if you are fired back into those stoneage days when using C++ ;) |
| ||
10 poke 53281,0 20 poke 53280,2 30 print "hello world This is C-64" think about this time :P sorry offtopic... |
| ||
SYS 64738Ooops |