469,913 Members | 2,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

track positions in arrays= index variables || pointers to elements?

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;

Nov 14 '05 #1
4 2103
Deniz Bahar wrote:
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;


Use whichever seems clearer for the given situation. After all, buff[n]
and *(buff + n) are identical.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
its another way of looking at it.
After all, the compiler too calculates the actual addresses of the
array parts notified by indices using the base address.

array[index] is equivalent to *(array + index * (sizeof(array_type)) )
Deniz Bahar wrote:
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;


Nov 14 '05 #3
Deniz Bahar wrote:
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?'

Are you sure K&R2 didn't just do that early on in the book because
pointers weren't covered yet?

--------------
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;

This instrument for holding a position holds a lot of information. It
has a reference type equal to the type of each member of the buffer,
and it holds an address of a member of the buffer. It does not require
a "base" address for use, or any other variables/values.

/* 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;

This instrument for holding a position holds much less information.
It's type is unrelated to the type of the array. It does not hold a
value that has any meaning on it's own. It can't do it's work without
being part of a larger expression that provides the base address for
the array.

/* example of use */
buffer[position++] = VALUE;

I prefer the pointer, because it can be readily passed around as
arguments to functions and hold enough information on it's own for
those functions to access the array. This allows functions to only
require 2 arguments (pointer and array size), instead of three (offset
integer, &array[0], size) - for strings the size isn't required.

Notice, library functions (like strstr strchr, etc.) usually return a
pointer to the actual element, instead of an offset. Imagine if strstr
returned an offset, then you'd have to capture this value into an
interger object, then use it with a pointer to the first character of
the string for dereferencing. As it is, you can simply use the return
value (without even storing it in a temp variable) to do many
meaningful things.

example:

if(strchar(string, '\o') == string)
{
puts("empty string");
}

That's the beauty of a pointer to an array element, it holds a enough
information to be useful on its own.

Nov 14 '05 #4

"Luke Wu" <Lo***********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

<snip>
Method 1: Pointers
===================

<snip>

This instrument for holding a position holds a lot of information. It
has a reference type equal to the type of each member of the buffer,
and it holds an address of a member of the buffer. It does not require
a "base" address for use, or any other variables/values.
However, on the downside, this version is not relocatable and cannot be used
to denote specific entries in a common data-block between two systems or two
runs of the same program.
Method 2: Index Ints
====================


<snip>
This instrument for holding a position holds much less information.
It's type is unrelated to the type of the array. It does not hold a
value that has any meaning on it's own. It can't do it's work without
being part of a larger expression that provides the base address for
the array.
On the upside, this version can readily be used to denote a position in a
block of data between two systems or two runs of the same program.
I prefer the pointer, because it can be readily passed around as
arguments to functions and hold enough information on it's own for
those functions to access the array.


Which is a perfectly good reason, but not the exclusive criteron. There are
other uses in which a integer offset is much preferable, especially if the
reference needs to be interchangable between system or different runs of the
same program. Since we can readily switch between the two, the choice for
the actual version to be used is not very critical.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Charles Banas | last post: by
3 posts views Thread by Randy Yates | last post: by
36 posts views Thread by raphfrk | last post: by
6 posts views Thread by Mark Broadbent | last post: by
12 posts views Thread by divya | last post: by
35 posts views Thread by Andrew Fabbro | last post: by
127 posts views Thread by sanjay.vasudevan | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.