What is **

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

    What is **

    Hi all,
    i have a question.
    What does those ** mean???
    For example:

    void function(Someth ing **pSomething);

    I hope someone can help me about it.

    Thanks,
    FroxX
  • Moonlit

    #2
    Re: What is **

    Hi,

    "FroxX" <flowo3@aol.com > wrote in message
    news:2004090406 0507.17257.0000 0020@mb-m25.aol.com...[color=blue]
    > Hi all,
    > i have a question.
    > What does those ** mean???
    > For example:
    >
    > void function(Someth ing **pSomething);[/color]

    It is a pointer to , a pointer to something.

    This is usually done when you want to assign something to a pointer for
    instance

    #include <stdlib.h>
    #include <memory.h>


    void MyAlloc( void **ptr )
    {
    *ptr = malloc( 1000 );
    }


    int main()
    {
    void *Ptr=0;
    MyAlloc( &Ptr );

    // do something not so useful
    memset( Ptr, 0, 1000 );

    free( Ptr);

    return 0;
    }

    [color=blue]
    >
    > I hope someone can help me about it.
    >
    > Thanks,
    > FroxX[/color]


    Comment

    • Boris Glawe

      #3
      Re: What is **

      FroxX wrote:[color=blue]
      > Hi all,
      > i have a question.
      > What does those ** mean???
      > For example:
      >
      > void function(Someth ing **pSomething);
      >
      > I hope someone can help me about it.
      >
      > Thanks,
      > FroxX[/color]


      A common example where this is used is the main function

      #include <iostream>

      int main(int argc, char** argv){
      //[...]
      }

      In this case char** argv can be seen as an object containing many strings.

      In C every array can by described as a pointer:
      char array[] = "hello world";
      is the same as
      char* p_array = array;

      The name of the array is the same as a pointer to the first element of the array.

      As you probably know, a string is an array of chars in C.

      If you want to pass many strings (which means many arrays of chars) as an
      argument to a function for example (as it also happens with the main function),
      you can store the pointers to each string in another array.

      You will then have an array of pointers. (Again: Each of these pointers point to
      an array, representing a string )
      This array can be seen like this;

      char* my-strings[] = { "hello world", "bye world" };

      This pointer is the same as the array above

      char** p_my-strings = my-strings;

      because char** p_my-string is a pointer to the first element of char*
      my-string[], which is a represenation of an array, as I wrote above. In this
      case it represents an array of pointers to arrays.

      Though this seems a bit confusing, it isn't that hard to understand.

      Don't imagine an array as many elements in a row, but as an object. If you use
      pointers to the first element of an array as representation of the array, you in
      fact have one object only - the pointer. The only case, you should imagine an
      array as a list of elements, is when you access the elements in the array.

      Again, if you have an array of arrays, you have a pointer, to many pointers,
      which is why it's written as char**



      The version char** is needed, if you dynamically allocate memory with malloc(),
      since malloc always returns pointers to the memory it has reserved. The returned
      pointer is the only way to access the reserved memory. This programming
      technique is important if you want to programm in a secure and resource friendly
      way.

      pointers to pointers are not limited to chars. This can be done with any kind of
      datatype, including structs.

      a pointer to a pointer is not necessarily an array of arrays, but this is what
      it's used for in 90% of all cases. I actually don't know a case where else you
      need pointers to pointers.

      If you are programming a table (image you were the programmer of EXCEL). Then
      each field of the table might be a pointer to a struct, which represents the Field.
      The table then consists of pointers to pointers to pointers, why you'd say
      Field*** table;


      greets Boris



      Comment

      • FroxX

        #4
        Re: What is **

        hi Boris,

        wie ich sehe bist du aus deutschland.
        Danke für die ausführliche Erklärung!!!

        //In English:
        I see you're from Germany.
        Thanks for the description!!!

        CU, FroxX
        FroxX, Germany, NRW, C++ Beginner, Froxxinating.de (excuse it, but i didnt work
        on it for long) :o)

        Comment

        Working...