Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: Which scripting for embedded automation? Date: Mon, 27 Jul 2015 20:32:26 -0400 Organization: Aioe.org NNTP Server Lines: 112 Message-ID: References: <0e930740-22d8-4e2b-833f-bb5843437d0d@googlegroups.com> <87y4i6rvyc.fsf@jester.gateway.sonic.net> <87lhe5sciz.fsf@jester.gateway.sonic.net> <8f8f8dd6-efca-4f3e-ac18-ed28bce7524c@googlegroups.com> <36adnY7PUqw6dCjInZ2dnUU7-KednZ2d@supernews.com> <8ab63cb4-00f9-496e-ac86-68ab37c4c653@googlegroups.com> NNTP-Posting-Host: n4wpt9zq8xR26Ttf9mo2BA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:40860 On Mon, 27 Jul 2015 19:44:26 -0400, Julian Fondren wrote: > On Monday, July 27, 2015 at 6:31:14 PM UTC-5, Rod Pemberton wrote: >> The big problem with Forth is there is no ability to know >> the size of the data the pointer points to. I.e., you'd need [] >> for cell size data and c[] for character sized data. > > The big problem with C is that you have to know the size of the > data that a pointer points to. No, you don't. The subscript operator [] in C works with the size of the object the pointer points to, as long as it's not a void pointer. For a[b] either a or b must be a pointer and the other must be an offset or index. If b is the offset, then a[b] is identical to: *(*a)((a)+((b)x(sizeof(*a)))) That's index b multiplied by the size of the object the pointer a points to, i.e., scale = index x size, plus the pointer a, the sum of which is type cast to the same type as pointer a, then it's dereferenced to fetch the appropriately sized value. Please note that the index or offset is automatically scaled by the size of the appropriate type. This is usually represented in C standards in simplied form without the scaling shown: E1[E2] equivalent to *((E1)+(E2)) Although the subscript operator [] has the appearance of an array index, it doesn't have to be used with just declared arrays. It can be used with any pointer in C or any object type which "decays into" a pointer such as declared arrays and parameters to functions. The is also no specification on which element must be the offset and which must be the pointer. E.g., /* c is set to the char at index 1, i.e., 'S' */ /* c indexing starts at zero */ /* "ASDF" is not a character constant but a name for a pointer to char */ /* ASDF\0 is the actual character constant which is stored elsewhere */ c = "ASDF"[1] /* pointer_to_char[index] */ > You can't just say [] and get it. What? Sure you can. int myint, myintarray*,myintsecond[5]={1,2,3,4,5}; char mychar, mychararray*,mycharsecond[]="Hello"; mychar=mycharsecond[3]; myint=myintsecond[4]; mychar will be set to the 4th char in the array "mychararray". myint will be set to the 5th int in the array "myintarray". You can do the same with pointers instead of arrays. myintarray= ... ; /* assign pointer to something */ mychararray= ... ; myint=myintarray[5]; mychar=mychararray[3]; I.e., the index is scaled by the appropriate object size for both arrays and pointers. The C subscript operater [] "knows" nothing about arrays. Except for declarations, C doesn't actually have arrays. The behavior of arrays is emulated purely by the subscript operator and the addresses C uses to identify objects. > You have to say what size it is when you get it: > > void example (float *fs) { > > and you have to say what size variable you're fetching it to: > > float c = *fs; No, you don't. At least, not where you placed "float". "float" would normally be used where "c" is declared, not where it is assigned, unless you're using C++ or a C99 feature. I.e., normal C code to dereference a pointer to an object: float c; /* declare c to be type float */ /* bunch of code ... */ c=*fs; /* dereference fs to fetch float, store in c */ > Just look at that last example again, next to the Forth: > > float c = *fs; > fs f@ > > Isn't it just awful, that one of these examples requires you to know > *so* much more than the other does? Why can't that one language be > as smart, on your behalf, as the other? I'm not sure about the use of floats in C, as I don't use them much. But, you don't have to do what you did that way in C for other C object types, structs, or unions. You can if you want though ... Rod Pemberton -- Scientists now say we'll experience a mini-ice in 2030. So, I guess we need more global warming today ...