472,364 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 2238
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
6
by: Mark Broadbent | last post by:
hi all. Ive been looking into the use of pointers to try and get my head around them which has led me onto a question. 1. How do you know what memory position C# compiler will use for each...
12
by: divya | last post by:
1. If The size of the array is unknown during programming time then how is such array declared?? Suppose there is an Array named arrClashname(size unknown at start), Now when the clash occurs...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
35
by: Andrew Fabbro | last post by:
I have a need to create an array that is larger than size_t - 1, which I believe is the largest guaranteed array. I though I'd found salvation in FAQ 6.14, but it appears I am not understanding...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.