Declaration syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRS Kumar

    Declaration syntax

    int* p[10] would mean that p is an array of 10 pointers to int.

    I was a little puzzzled when I came across the following:

    int (*p)[10];


    What exactly does this one mean?


    Thank you for your time.
  • Leor Zolman

    #2
    Re: Declaration syntax

    On 9 Apr 2004 16:16:25 -0700, lrsk1260@yahoo. co.in (LRS Kumar) wrote:
    [color=blue]
    >int* p[10] would mean that p is an array of 10 pointers to int.
    >
    >I was a little puzzzled when I came across the following:
    >
    >int (*p)[10];
    >
    >
    >What exactly does this one mean?[/color]

    p is a pointer to an array of 10 ints.

    The vast majority of the times when you wish to point to an array, you do
    it by defining a pointer to the element type, and setting that pointer to
    point to the first element of the array:

    int a[10];
    int *pi;
    ...
    pi = a; // same as: pi = &a[0];

    Using a bona-fide pointer-to-array comes into use more when having to point
    to multi-dimensional arrays, which C/C++ does not support in any especially
    friendly way. I could not find one example (and I tried for a while) of
    when a pointer to a 1-dimensional array of T's would be preferable to just
    using a pointer-to-T, but I've no doubt there must be at least one
    somewhere (well, almost no doubt; if no one provides an example here, try
    comp.lang.c). In the meantime, for a good tutorial document on declarations
    that includes quite a bit more than you'd typically expect to find on
    pointer-to-arrays, check this out:


    HTH,
    -leor
    [color=blue]
    >
    >
    >Thank you for your time.[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: Declaration syntax

      On 9 Apr 2004 16:16:25 -0700, lrsk1260@yahoo. co.in (LRS Kumar) wrote:
      [color=blue]
      >int* p[10] would mean that p is an array of 10 pointers to int.
      >
      >I was a little puzzzled when I came across the following:
      >
      >int (*p)[10];
      >
      >
      >What exactly does this one mean?[/color]

      p is a pointer to an array of 10 ints.

      The vast majority of the times when you wish to point to an array, you do
      it by defining a pointer to the element type, and setting that pointer to
      point to the first element of the array:

      int a[10];
      int *pi;
      ...
      pi = a; // same as: pi = &a[0];

      Using a bona-fide pointer-to-array comes into use more when having to point
      to multi-dimensional arrays, which C/C++ does not support in any especially
      friendly way. I could not find one example (and I tried for a while) of
      when a pointer to a 1-dimensional array of T's would be preferable to just
      using a pointer-to-T, but I've no doubt there must be at least one
      somewhere (well, almost no doubt; if no one provides an example here, try
      comp.lang.c). In the meantime, for a good tutorial document on declarations
      that includes quite a bit more than you'd typically expect to find on
      pointer-to-arrays, check this out:


      HTH,
      -leor
      [color=blue]
      >
      >
      >Thank you for your time.[/color]

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      Working...