D
Deniz Bahar
Hello all,
Often times programs in C have arrays used as buffers and shared among
different sections of code. The need arises to have position
indicators to point to different parts of an array (example: point to
top of stack). Before I even got K&R2 I used to just define extra
pointers to types equal to the element type of the arrays to act as
indicators. Now flipping through K&R2, I see they use int variables to
act as "offsets." What do you experts think of these two methods?
What would be the advatages the "better" method has over the other?
--------------
Example of what I am talking about in question above (SIZE, VALUE are
symbolic constants).
Method 1: Pointers
===================
/* array used as a buffer shared among functions */
T buffer[SIZE];
/* pointer used to point to positions(elements) in buffer */
T *position;
/* example of use */
*position++ = VALUE;
Method 2: Index Ints
====================
/* array used as a buffer shared among functions */
T buffer[SIZE];
/* int used to designate offset from first element of buffer */
int position;
/* example of use */
buffer[position++] = VALUE;
Often times programs in C have arrays used as buffers and shared among
different sections of code. The need arises to have position
indicators to point to different parts of an array (example: point to
top of stack). Before I even got K&R2 I used to just define extra
pointers to types equal to the element type of the arrays to act as
indicators. Now flipping through K&R2, I see they use int variables to
act as "offsets." What do you experts think of these two methods?
What would be the advatages the "better" method has over the other?
--------------
Example of what I am talking about in question above (SIZE, VALUE are
symbolic constants).
Method 1: Pointers
===================
/* array used as a buffer shared among functions */
T buffer[SIZE];
/* pointer used to point to positions(elements) in buffer */
T *position;
/* example of use */
*position++ = VALUE;
Method 2: Index Ints
====================
/* array used as a buffer shared among functions */
T buffer[SIZE];
/* int used to designate offset from first element of buffer */
int position;
/* example of use */
buffer[position++] = VALUE;