pointers #2

  • Follow


Which is the correct way, to initialise a pointer to an element of
an array. I am sorry if my terminology is not correct

/* declare pointer */
int *ptr;
/* define and initialise array */
int array[10] = {,0,1,2,3,4,5,6,7,8,9}; 
/* assign value to pointer */
ptr = &array[9];  or  ptr = (array + 9);

I have found both work but to avoid future problems is one
better than the other. And if so why

0
Reply nglen7024024 (2) 1/11/2004 1:23:07 PM

darklight wrote:

> Which is the correct way, to initialise a pointer to an element of
> an array. I am sorry if my terminology is not correct
> 
> /* declare pointer */
> int *ptr;
> /* define and initialise array */
> int array[10] = {,0,1,2,3,4,5,6,7,8,9};
> /* assign value to pointer */
> ptr = &array[9];  or  ptr = (array + 9);
> 
> I have found both work but to avoid future problems is one
> better than the other. And if so why

The two expressions are equivalent. Each points ptr to the last element of 
the array.

In a value context, the following pairs of expressions are equivalent:

  (arrayname)        ==   (&arrayname[0])
  (arrayname + n)    ==   (&arrayname[n])
  (*(arrayname + n)) ==   (arrayname[n])

Which you choose is entirely a matter of taste.

I should perhaps add that the external parentheses are there purely as an 
attempted defence against nitpicking! :-)

-- 
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
0
Reply dontmail (1884) 1/11/2004 1:46:24 PM



darklight wrote:
> Which is the correct way, to initialise a pointer to an element of
> an array. I am sorry if my terminology is not correct
> 
> /* declare pointer */
> int *ptr;
> /* define and initialise array */
> int array[10] = {,0,1,2,3,4,5,6,7,8,9}; 
> /* assign value to pointer */
> ptr = &array[9];  or  ptr = (array + 9);
> 
> I have found both work but to avoid future problems is one
> better than the other. And if so why
> 

Either way will be valid. Generally you will find the [] operator
the most accepted method. To me, it it easier to understand the
operations.

By the way the () operator is not required in the above assignment,
but you may add them if it gives you more clarity. It could be,
ptr = array + 9;

However, in dereferencing, you will need the () operator to yield
the value of the last element of the array named array.
int i = *(array + 9;) will assign to i the value 9.
int i = *array + 9; will assign to i the value 18.

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

0
Reply xabowers (230) 1/11/2004 1:48:01 PM

darklight <nglen702@netscapel.net> wrote:
# Which is the correct way, to initialise a pointer to an element of
# an array. I am sorry if my terminology is not correct
# 
# /* declare pointer */
# int *ptr;
# /* define and initialise array */
# int array[10] = {,0,1,2,3,4,5,6,7,8,9}; 
# /* assign value to pointer */
# ptr = &array[9];  or  ptr = (array + 9);
# 
# I have found both work but to avoid future problems is one
# better than the other. And if so why

&(array[9]) = &(*(array+9)) = (array+9)

The notations are equivalent, and you can expect a compiler to generate
the exact same code for both. Which is better is style question for you.

Which do you think looks better?
Which do you more easily understand?

That is the correct one.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Don't say anything. Especially you.
0
Reply derkgwen (343) 1/11/2004 1:50:11 PM

darklight <nglen702@netscapel.net> wrote:

> Which is the correct way, to initialise a pointer to an element of
> an array. I am sorry if my terminology is not correct
> 
> /* declare pointer */
> int *ptr;
> /* define and initialise array */
> int array[10] = {,0,1,2,3,4,5,6,7,8,9}; 
> /* assign value to pointer */
> ptr = &array[9];  or  ptr = (array + 9);
> 
> I have found both work but to avoid future problems is one
> better than the other. And if so why

Either one works equally well and the decision on which to use is
generally based on personal preference and the context.


-- 
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both 
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
0
Reply egDfAusenetE5fz (78) 1/11/2004 3:05:13 PM

Hi

   ptr=&array[9] 0r (array+9) both refer same operation. The basic difference
is that by accessing with pointers the operation will be quite faster while
comparing array[9]. Hence good C programmers will prefer pointers. Hope this 
information will be useful to u. If u find anymore idea like this please let me
know. 

Regards,
Anand.
0
Reply anandattek (5) 1/12/2004 10:53:40 AM

Anand wrote:
> 
> Hi
> 
>    ptr=&array[9] 0r (array+9) both refer same operation.
> The basic difference is that by accessing with pointers
> the operation will be quite faster while comparing array[9].

Unless it isn't faster. 

The compiler has enough information to know that
(&array[9]) and (array+9) are the same.

I'm unfamiliar with the address operator as being translated 
into a time consuming machine operation.

-- 
pete
0
Reply pfiland (6613) 1/12/2004 11:16:04 AM

On 12 Jan 2004 02:53:40 -0800, anandattek@yahoo.co.in (Anand) wrote:

> Hi
> 
>    ptr=&array[9] 0r (array+9) both refer same operation. The basic difference
> is that by accessing with pointers the operation will be quite faster while
> comparing array[9]. Hence good C programmers will prefer pointers. Hope this 
> information will be useful to u. If u find anymore idea like this please let me
> know. 

My compilers generate the same code for

	p = &array[9]

and

	p = array+9

What about your compilers?

Regards
Horst

0
Reply horst.kraemer (18) 1/12/2004 12:13:24 PM

Anand wrote:

> Hi
> 
>    ptr=&array[9] 0r (array+9) both refer same operation. The basic
>    difference
> is that by accessing with pointers the operation will be quite faster
> while comparing array[9].

Get A Better Compiler. Yours Is Broken.


> Hence good C programmers will prefer pointers.

Better C programmers know that the compiler knows perfectly well that the 
two expressions are equivalent.

> Hope this information will be useful to u.

Let us hope that u, whoever he is, never reads it, since it's fundamentally 
flawed.

-- 
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
0
Reply invalid29 (585) 1/12/2004 2:18:19 PM

Eric <egDfAusenetE5fz@verizon.net> spoke thus:
> darklight <nglen702@netscapel.net> wrote:

>> /* declare pointer */
>> int *ptr;
>> /* define and initialise array */
>> int array[10] = {0,1,2,3,4,5,6,7,8,9}; 
                    ^ extra comma removed
>> /* assign value to pointer */
>> ptr = &array[9];  or  ptr = (array + 9);

> Either one works equally well and the decision on which to use is
> generally based on personal preference and the context.

But the equivalence breaks down for

ptr=array+10;   /* Legal */
ptr=&array[10]; /* Illegal (?), since array[10] is a dereference */

, right?

-- 
Christopher Benson-Manica  | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org    | don't, I need to know.  Flames welcome.
0
Reply ataru (1609) 1/12/2004 3:17:23 PM

Christopher Benson-Manica wrote:
> Eric <egDfAusenetE5fz@verizon.net> spoke thus:
>> darklight <nglen702@netscapel.net> wrote:
> 
>>> /* declare pointer */
>>> int *ptr;
>>> /* define and initialise array */
>>> int array[10] = {0,1,2,3,4,5,6,7,8,9}; 
>                     ^ extra comma removed
>>> /* assign value to pointer */
>>> ptr = &array[9];  or  ptr = (array + 9);
> 
>> Either one works equally well and the decision on which to use is
>> generally based on personal preference and the context.
> 
> But the equivalence breaks down for
> 
> ptr=array+10;   /* Legal */
> ptr=&array[10]; /* Illegal (?), since array[10] is a dereference */
> 
> , right?

Wrong, at least in C99.

   [6.5.3.2]
   3 The unary & operator returns the address of its operand. [...]
     Similarly, if the operand is the result of a [] operator, neither
     the & operator nor the unary * that is implied by the [] is
     evaluated and the result is as if the & operator were removed and
     the [] operator were changed to a + operator.

Jeremy.
0
Reply jeremy63 (294) 1/12/2004 4:21:39 PM

Jeremy Yallop <jeremy@jdyallop.freeserve.co.uk> spoke thus:

>    3 The unary & operator returns the address of its operand. [...]
>      Similarly, if the operand is the result of a [] operator, neither
>      the & operator nor the unary * that is implied by the [] is
>      evaluated and the result is as if the & operator were removed and
>      the [] operator were changed to a + operator.

Thank you, I am yet again abashed yet wiser :)

-- 
Christopher Benson-Manica  | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org    | don't, I need to know.  Flames welcome.
0
Reply ataru (1609) 1/12/2004 5:01:22 PM

In article <cdf5b6.0401120253.4d2b99ac@posting.google.com>,
 anandattek@yahoo.co.in (Anand) wrote:

> Hi
> 
>    ptr=&array[9] 0r (array+9) both refer same operation. 

   You should have just stopped there.

> The basic difference
> is that by accessing with pointers the operation will be quite faster while
> comparing array[9]. 

   Says who? I have *never* seen a compiler that generates more efficent 
code for (&array[9]) than it does for *(array+9).

0
Reply clarkcox31 (44) 1/13/2004 5:11:24 AM

12 Replies
23 Views

(page loaded in 0.223 seconds)


Reply: