pointer to an array of pointers

Discussion in 'C Programming' started by ramu, Dec 3, 2007.

  1. ramu

    ramu Guest

    Hi,
    Could anyone please tell me how to dereference a pointer to an
    array of pointers?

    Regards
     
    ramu, Dec 3, 2007
    #1
    1. Advertising

  2. ramu

    Guest

    On Dec 3, 7:13 pm, ramu <> wrote:
    > Hi,
    > Could anyone please tell me how to dereference a pointer to an
    > array of pointers?
    >
    > Regards


    *p or p[0].

    A pointer to an array of N pointers is char *(*)[N].

    Example:

    char *(*foo)[N] = NULL;
     
    , Dec 3, 2007
    #2
    1. Advertising

  3. "ramu" <> wrote in message
    news:...
    > Hi,
    > Could anyone please tell me how to dereference a pointer to an
    > array of pointers?
    >

    /* set up a pointer to a list of pointers, here strings for readability */
    char **strings;
    int i;

    strings = malloc(12 * sizeof(char *));
    for(i=0;i<12;i++)
    {
    strings = malloc(32);
    sprintf(strings, "string %d", i+1);
    }

    /* dereference to get a string, or char * */
    printf("%s\n", strings[3]);
    /* dereference to get a character, should be the letter g */
    printf("%c\n", strings[3][5]);

    As you can see, when you say "dereference the pointer" you can mean either
    get what it points to immediately, which is another pointer, or what it
    points to ultimately, which in this case is a char.

    Also we can use this syntax

    /* treat as pointer to single element */
    printf("%s\n", *strings);

    /* get first element of first element */
    printf("%c\n", **strings);

    This isn't so useful as the first. Usually when you want a pointer it is
    because you have an array of things to point to, pointers to single items
    are less common. However you'll need to know both syntaxes.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Dec 3, 2007
    #3
  4. ramu

    Guest

    On Dec 3, 11:20 pm, "Malcolm McLean" <> wrote:
    > "ramu" <> wrote in message
    >
    > news:...> Hi,
    > > Could anyone please tell me how to dereference a pointer to an
    > > array of pointers?

    >
    > /* set up a pointer to a list of pointers, here strings for readability */
    > char **strings;
    > int i;
    >
    > strings = malloc(12 * sizeof(char *));
    > for(i=0;i<12;i++)
    > {
    > strings = malloc(32);
    > sprintf(strings, "string %d", i+1);
    >
    > }
    >
    > /* dereference to get a string, or char * */
    > printf("%s\n", strings[3]);
    > /* dereference to get a character, should be the letter g */
    > printf("%c\n", strings[3][5]);
    >
    > As you can see, when you say "dereference the pointer" you can mean either
    > get what it points to immediately, which is another pointer, or what it
    > points to ultimately, which in this case is a char.

    When you dereference a pointer you mean access what it points to.
    In strings[3][5] you're dereferencing two pointers. strings and
    strings[3].

    > Also we can use this syntax
    >
    > /* treat as pointer to single element */
    > printf("%s\n", *strings);

    *strings is equal to strings[0] and it has nothing to do with 'single
    element'.
    A pointer is of scalar type.

    Really, i don't undestand your post, OP asked for a pointer to array
    of pointers and you answered with a char **?
     
    , Dec 3, 2007
    #4
  5. ramu

    CBFalconer Guest

    wrote:
    >

    .... snip ...
    >
    > Really, i don't undestand your post, OP asked for a pointer to
    > array of pointers and you answered with a char **?


    A char** is a pointer to a pointer to char, which is what a
    function will receive as a parameter to describe an array of
    pointers to char. Remember that under most conditions an array is
    described by a pointer to its zeroth element.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 4, 2007
    #5
  6. On Mon, 3 Dec 2007 09:13:14 -0800 (PST), ramu <>
    wrote:

    >Hi,
    > Could anyone please tell me how to dereference a pointer to an
    >array of pointers?
    >


    In strict terminology, a pointer to an array of pointers is
    TYPE *arr[N];
    TYPE *(*ptr)[N];
    ptr = &arr;
    ptr is a pointer to an array of N pointer to TYPE and points to one
    such array.

    In this case, ptr[0] is the array itself and ptr[0] is the i-th
    pointer in the array.

    Frequent, as in 99+%, newcomers to the language use the term to mean
    TYPE *arr[N]
    TYPE **ptr;
    ptr =arr; /* or the equivalent ptr = &arr[0]; */
    ptr is a pointer to pointer to TYPE and points to the first pointer in
    the array of such pointers.

    In this case, the slightly simpler ptr is the i-th pointer in the
    array.

    Which one did you mean?


    Remove del for email
     
    Barry Schwarz, Dec 6, 2007
    #6
    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. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    349
    Tim Rentsch
    Oct 9, 2004
  2. mann!

    pointer to array v/s array of pointers

    mann!, Feb 25, 2005, in forum: C Programming
    Replies:
    6
    Views:
    336
    E. Robert Tisdale
    Feb 26, 2005
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    676
    Martin Ambuhl
    Jan 28, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page