Array of pointer Vs Pointer to Array

Discussion in 'C Programming' started by sangeetha, Oct 8, 2004.

  1. sangeetha

    sangeetha Guest

    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.
    sangeetha, Oct 8, 2004
    #1
    1. Advertising

  2. In article <>,
    sangeetha <> 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
    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
    Dave Vandervies, Oct 8, 2004
    #2
    1. Advertising

  3. sangeetha

    Artie Gold Guest

    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."
    Artie Gold, Oct 8, 2004
    #3
  4. In article <>,
    (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
    Jonathan Adams, Oct 8, 2004
    #4
  5. sangeetha

    Default User Guest

    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
    Default User, Oct 8, 2004
    #5
  6. On 8 Oct 2004 07:58:40 -0700, (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>>
    Barry Schwarz, Oct 8, 2004
    #6
  7. sangeetha

    John Bode Guest

    (sangeetha) wrote in message news:<>...
    > 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.
    John Bode, Oct 8, 2004
    #7
  8. sangeetha

    Zian Smith Guest

    (sangeetha) wrote in message news:<>...
    > 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..
    Zian Smith, Oct 8, 2004
    #8
  9. sangeetha

    sangeetha Guest

    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.

    (Zian Smith) wrote in message news:<>...
    > (sangeetha) wrote in message news:<>...
    > > 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..
    sangeetha, Oct 9, 2004
    #9
  10. sangeetha

    Tim Rentsch Guest

    [The quoted material has been re-ordered into the order the pieces appeared.]

    (sangeetha) writes:

    > (Zian Smith) wrote in [...]
    > > (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[j]'
    and 'matrix[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_matrix[10][100];
    int *ap[10];
    int i;

    for( i = 0; i < 10; i++ ) ap = transposed_matrix;

    Now each of the pointers in 'ap' points to one of the 10 sub-arrays
    in 'transposed_matrix', and & ap[j] == & transposed_matrix[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.
    Tim Rentsch, Oct 9, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    695
    Chris Torek
    Feb 4, 2005
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    673
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,246
  4. Hansen
    Replies:
    3
    Views:
    1,098
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page