473,563 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of pointer Vs Pointer to Array

Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.

Regards,
Sangeetha.
Nov 14 '05 #1
9 15712
sangeetha wrote:
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers No, a pointer to an array of 10 `int's
int *ptr[10]; // pointer-to-array of 10.

No, an array of 10 pointers-to-int.

They are entirely different things.

HTH,
--ag
--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #2
In article <4f************ **************@ posting.google. com>,
sa*********@ind ia.com (sangeetha) wrote:
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.


Well, you've got their meanings backwards. Does that help?

- jonathan
Nov 14 '05 #3
In article <4f************ **************@ posting.google. com>,
sangeetha <sa*********@in dia.com> wrote:
Hello,

Is there any performance difference in using of the following two declaration?
Yes. One declares an array of pointers, one declares a pointer to
an array.
int (*ptr)[10]; //Array of 10 int pointers
Contrary to your comment, this declares ptr as a pointer to an array
of int:

ptr: some array, somewhere:
[ ] -> [ten ]
[ints]
[in ]
[a ]
[row ]
[ ]
[ ]
[ ]
[ ]
[ ]
int *ptr[10]; // pointer-to-array of 10.


Contrary to your comment, this declares ptr as an array of pointers:

ptr: floating around in various places:
[ten ] -> [some int]
[pointers] ------> [some other int] <---+
[in ] -> NULL, perhaps |
[a ] ------> [yet another int] |
[row ] ---two-pointers-to-same-int--+
[ ] ------> Maybe another NULL?
[ ] -> you can complete the pattern
[ ] ->
[ ] ->
[ ] ->
Google for posts by Chris Torek discussing arrays and pointers for
further enlightenment.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
I can easily picture some Chinese civil servant in $DYNASTY complaining
about the cheap government abaci he has to fix.
--Matt Roberds in the scary devil monastery
Nov 14 '05 #4
sangeetha wrote:
Hello,

Is there any performance difference in using of the following two
declaration? int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.

They're completely different things, so who cares? As they are not
interchangable, use the correct one.

Brian Rodenborn

Nov 14 '05 #5
On 8 Oct 2004 07:58:40 -0700, sa*********@ind ia.com (sangeetha) wrote:
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.


Comments are incorrect. Reverse them.

What happens when you dereference the variable?

In case 1, *ptr evaluates to an array of 10 int.

In case 2, *ptr evaluates to the first pointer-to-int of the 10
defined.
<<Remove the del for email>>
Nov 14 '05 #6
sa*********@ind ia.com (sangeetha) wrote in message news:<4f******* *************** ****@posting.go ogle.com>...
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.


Other way around.

int (*ptr)[10]; // pointer to 10-element array of int
int *ptr[10]; // 10-element array of pointer to int.

These are two completely different entities, used for completely
different purposes. Any difference in performance between the two (by
whatever metric) is meaningless.
Nov 14 '05 #7
sa*********@ind ia.com (sangeetha) wrote in message news:<4f******* *************** ****@posting.go ogle.com>...
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.

Regards,
Sangeetha.


Well, first you have it mixed up:

int (*ptr)[10]; //pointer-to-array of 10 ints
int* ptr[10]; //array of 10 pointers (to ints)

Second, they are essentially different structures, the first is simply
a single pointer, that points to an array. The second is an array that
contains 10 pointers.. If you can post an example of how you want to
use either of these, or what you are trying to accomplish, that might
be provide us with more information for comments regarding
performance/usage..
Nov 14 '05 #8
Thx for every one, replied to my query. First of all it was my typo.

Last week i attended one interview. I have explained correctly, by
putting the diagram how it stores. Then he asked "wht is the
performance difference between these two when using in the program". I
said "These two are eniterly different things, cant be weighted". He
didnt convenience on my answer.

I was curious to know whether it can weigthed for performance point.
"C" experts in this forum also conformed me it cant be.

sm*******@yahoo .com (Zian Smith) wrote in message news:<2a******* *************** ****@posting.go ogle.com>...
sa*********@ind ia.com (sangeetha) wrote in message news:<4f******* *************** ****@posting.go ogle.com>...
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.

Regards,
Sangeetha.


Well, first you have it mixed up:

int (*ptr)[10]; //pointer-to-array of 10 ints
int* ptr[10]; //array of 10 pointers (to ints)

Second, they are essentially different structures, the first is simply
a single pointer, that points to an array. The second is an array that
contains 10 pointers.. If you can post an example of how you want to
use either of these, or what you are trying to accomplish, that might
be provide us with more information for comments regarding
performance/usage..

Nov 14 '05 #9
[The quoted material has been re-ordered into the order the pieces appeared.]

sa*********@ind ia.com (sangeetha) writes:
sm*******@yahoo .com (Zian Smith) wrote in [...]
sa*********@ind ia.com (sangeetha) wrote in [...]
Hello,

Is there any performance difference in using of the following two declaration?
int (*ptr)[10]; //Array of 10 int pointers
int *ptr[10]; // pointer-to-array of 10.

Regards,
Sangeetha.
Well, first you have it mixed up:

int (*ptr)[10]; //pointer-to-array of 10 ints
int* ptr[10]; //array of 10 pointers (to ints)

Second, they are essentially different structures, the first is simply
a single pointer, that points to an array. The second is an array that
contains 10 pointers.. If you can post an example of how you want to
use either of these, or what you are trying to accomplish, that might
be provide us with more information for comments regarding
performance/usage..

Thx for every one, replied to my query. First of all it was my typo.

Last week i attended one interview. I have explained correctly, by
putting the diagram how it stores. Then he asked "wht is the
performance difference between these two when using in the program". I
said "These two are eniterly different things, cant be weighted". He
didnt convenience on my answer.

I was curious to know whether it can weigthed for performance point.
"C" experts in this forum also conformed me it cant be.


My guess is people were thrown off by what seemed to be a lack of
experience in the original posting. Certainly it didn't help that the
two declarations had their types misidentified in the comments.

In fact, however, there is a sense in which these two variables can
meaningfully be compared: they both can be used as "two dimensional
arrays".

The pointer to array case -- int (*pa)[10]; -- is a little easier to
understand; consider for example:

int matrix[100][10];
int (*pa)[10] = matrix[0];

Now 'pa' is a pointer form of accessing the values in 'matrix'. Indeed
something like 'pa' is what one would normally use in the case where
'matrix' were being passed to a function. The expressions 'pa[i][j]'
and 'matrix[i][j]' access the same (int) element.

The array of pointers case -- int *ap[10]; -- is a somewhat less
easy to see immediately. Here is an example:

int transposed_matr ix[10][100];
int *ap[10];
int i;

for( i = 0; i < 10; i++ ) ap[i] = transposed_matr ix[i];

Now each of the pointers in 'ap' points to one of the 10 sub-arrays
in 'transposed_mat rix', and & ap[i][j] == & transposed_matr ix[i][j]
provided of course i and j are in the correct respective ranges.

So both 'pa' and 'ap' may be used as two-dimensional arrays, and
may reasonably be compared in that context.

Now, to the question. Performance difference between the two? Well
that depends on a variety of things - the size of the other dimension,
are accesses typically done varying the "10" dimension or the other
dimension most rapidly, are accesses typically sequential or typically
random, how good the compiler is at optimizing each of the two kinds
of access, to name some of the most obvious. For any particular piece
of code, either of the two types might yield better performance, and
conversely, if either of the two types were imposed by some external
constraint, a good C expert could probably re-work the code so that
the performance of the imposed type were better than the performance
of the other type. But basically it depends.

In the absence of other information, and especially in something like
an interview question, the form that is more like a regular array (in
other words the pointer-to-array 'pa' choice) is more likely to be
seen as the right answer. The two main reasons for this are, (1) less
indirection, and (2) compilers are generally better at optimizing
ordinary two-dimensional indexing arithmetic than they are at
optimizing the "index, follow a pointer and index again" kind of
operation that is used in the array-of-pointers 'ap' choice.

If it were me doing the interviewing, I'd be reasonably happy with
the answer in the last paragraph - the regular indexing style is
likely to be better, because it's more like what C typically does.
I'd be happier though to see an answer like "it depends; normally
the regular indexing style is a good first guess, but in an actual
situation other factors might dominate" and then list some factors
like those listed above. This is a person who thinks more deeply,
and who understands that performance questions are often difficult
to answer in the absence of specific details or measurement.

The question posed is not a trivial one. The way it was phrased in
the original article probably caused most people to just dismiss it.

Nov 14 '05 #10

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

Similar topics

58
10077
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
5
3427
by: Richard Delorme | last post by:
The n869 draft says: J.2 Undefined behavior The behavior is undefined in the following circumstances: -- An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a given the declaration
9
2303
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
11
4434
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
204
12937
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
1
3083
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
8
10701
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type...
1
617
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a platform which has the following properties: 1) char's are 8-Bit. ( "char" is synomonous with "byte" ). 2) int's are 32-Bit. ( sizeof(int) == 4 )....
12
3861
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm...
26
4831
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
1
7638
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
7948
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
6250
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
5484
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
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
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
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.