473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

pointers

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

Nov 14 '05 #1
12 6118
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 : bi****@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
Nov 14 '05 #2


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: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
darklight <ng******@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.
Nov 14 '05 #4
darklight <ng******@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... ===
Nov 14 '05 #5
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.
Nov 14 '05 #6
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
Nov 14 '05 #7
On 12 Jan 2004 02:53:40 -0800, an********@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

Nov 14 '05 #8
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 : bi****@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
Nov 14 '05 #9
Eric <eg*************@verizon.net> spoke thus:
darklight <ng******@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.
Nov 14 '05 #10
Christopher Benson-Manica wrote:
Eric <eg*************@verizon.net> spoke thus:
darklight <ng******@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.
Nov 14 '05 #11
Jeremy Yallop <je****@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.
Nov 14 '05 #12
In article <cd************************@posting.google.com>,
an********@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).

Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.