pointer to an array of pointers

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

    #1

    pointer to an array of pointers

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

    Regards
  • vippstar@gmail.com

    #2
    Re: pointer to an array of pointers

    On Dec 3, 7:13 pm, ramu <ramu....@gmail .comwrote:
    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;

    Comment

    • Malcolm McLean

      #3
      Re: pointer to an array of pointers


      "ramu" <ramu.ask@gmail .comwrote in message
      news:f0977e82-d97d-4124-8f94-f3addd5b6c8a@s8 g2000prg.google groups.com...
      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[i] = malloc(32);
      sprintf(strings[i], "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 "dereferenc e 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.


      Comment

      • vippstar@gmail.com

        #4
        Re: pointer to an array of pointers

        On Dec 3, 11:20 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
        "ramu" <ramu....@gmail .comwrote in message
        >
        news:f0977e82-d97d-4124-8f94-f3addd5b6c8a@s8 g2000prg.google groups.com...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[i] = malloc(32);
        sprintf(strings[i], "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 "dereferenc e 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 **?

        Comment

        • CBFalconer

          #5
          Re: pointer to an array of pointers

          vippstar@gmail. com 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

          Comment

          • Barry Schwarz

            #6
            Re: pointer to an array of pointers

            On Mon, 3 Dec 2007 09:13:14 -0800 (PST), ramu <ramu.ask@gmail .com>
            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][i] 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[i] is the i-th pointer in the
            array.

            Which one did you mean?


            Remove del for email

            Comment

            Working...