473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6194
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.pow ernet.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******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
darklight <ng******@netsc apel.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******@netsc apel.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********@yaho o.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.pow ernet.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******@netsc apel.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

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

Similar topics

27
3368
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 as ff: typedef struct { float *data ; int count ;
3
3436
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, or one-past the end of the object as long as it isn't dereferenced. I use "object" in the standard 'C' sense. Is there some special dispensation...
9
5047
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 = getter(file); What type should I give to `getter' so that the compiler does not issue
12
4070
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 VB.NET get compiled into the same code, then I don't understand why VB.NET can't support pointers Thanks for any info Lance
14
2811
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 contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
92
5029
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, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
4
3497
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 vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
25
12999
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any...
54
11918
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 the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously...
2
2968
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress....
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.