|
Hello James, Answers in line below .... Regards, Simon Coulter. «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» FlyByNight Software AS/400 Technical Specialists «» «» Eclipse the competition - run your business on an IBM AS/400. «» «» «» «» Phone: +61 3 9419 0175 Mobile: +61 0411 091 400 «» «» Fax: +61 3 9419 0175 mailto: shc@flybynight.com.au «» «» «» «» Windoze should not be open at Warp speed. «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» //--- forwarded letter ------------------------------------------------------- > Date: Fri, 23 Jun 2000 16:38:59 -0600 > From: "James David Rich" <james@dansfoods.com> > To: rpg400-l@midrange.com > Reply-To: RPG400-L@midrange.com > Subject: pointers > > Can someone explain the difference between a basing pointer in RPG and a > pointer in C? An RPG basing pointer has no type. It is like a void pointer in C. It just points to 'stuff'. You determine how that 'stuff' appears by defining a variable, structure, or array and basing it on the pointer. The basing pointer locates the storage, the based variable defines the view of the storage. That means that you cannot increment a structure pointer and have it move up in storage by the size of the structure. You have to increment the pointer by the actual number of bytes. For example, you can't do this in RPG: typedef struct { char stuff[100]; int stuffLen; } var_t; var_t * ptr; /* Declare a pointer of type var_t */ ptr = malloc( (sizeof(var_t) * 10) ); /* Allocate storage for 10 structures */ ptr++; /* point to next structure */ That's actually a good thing when dealing with the system APIs because you should never increment the pointer by the size of the structure, rather you should use the entry length returned by the API (usually in the header for the various user space APIs. However you can do something like: ptr = ptr + sizeof(var_t); if you need to. The RPG equivalent is: EVAL ptr = ptr + %size(data) >Is there some other kind of pointer besides a basing > pointer in RPG? RPG also supports a procedure pointer which is similar to a function pointer in C. > > Can someone explain how to do write the following C code in RPG: > > void > main (){ > char *buffer; > > buffer = (char *) malloc (sizeof(char) * 1024); > return; > } > Aside from the C-style issues .... what you have below will allocate 1K of storage but there is not much you can do with it like that because you can't dereference the pointer. You must use one or more based variables -- see the next example. > I know that I need to do something like: > > D ptr S * > C alloc(e) 1024 ptr > > but now how do I specify that ptr points to a buffer of type char? Even > better: how do I use alloc to allocate memory that looks like this: > > D data DS > D field 100A D data DS BASED(@data) D field 100A D size S 5I 0 C EVAL size = %size(data) C ALLOC(E) size @data Now you can do stuff with field or with data simply by referencing the names. Better still is to wrap the allocation op-codes in procedures (or simply use the C runtime versions) so you can write the code like: C EVAL @data = allocStg(%size(data)) C EVAL @data = reallocStg(%size(data) * 10) C CALLP deallocStg(@data) > > basically I want a 2-dimensional array that I can dynamically increase or > descrease. You will need to manage the array dimensions yourself but you can use based arrays in a similar fashion to the above examples. REALLOC will increase allocated space and DEALLOC will release the storage. > > James Rich > james@dansfoods.com +--- | This is the RPG/400 Mailing List! | To submit a new message, send your mail to RPG400-L@midrange.com. | To subscribe to this list send email to RPG400-L-SUB@midrange.com. | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com. | Questions should be directed to the list owner/operator: david@midrange.com +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.