how to define this complex declaration and how to use it

Discussion in 'C Programming' started by Cric, Sep 5, 2007.

  1. Cric

    Cric Guest

    Hi,

    how can be the declaration of

    a pointer which points to dynamic array of pointers and each pointer
    from this array points to an dynamic array of pointers which points to
    character strings

    p-->p-->p
    p
    p-->p
    p
    p
    p-->p
    p
    p
    p

    How to use such storage to fill the strings and then retrieve. Does
    anybody has the example
    upto this deep pointing algorithm.

    Thanks,

    Cric
    Cric, Sep 5, 2007
    #1
    1. Advertising

  2. Cric

    Army1987 Guest

    On Tue, 04 Sep 2007 21:08:45 -0700, Cric wrote:

    > Hi,
    >
    > how can be the declaration of
    >
    > a pointer which points to dynamic array of pointers and each pointer
    > from this array points to an dynamic array of pointers which points to
    > character strings


    Usually you'd better have a pointer to the first element of an
    array than to the whole array, especially if its size is unknown
    at compile time. So you want a pointer to pointer to char, or
    pointer to pointer to pointer to char, depending on what you mean
    by "points to character strings".
    So: char **ptr; or char ***ptr.

    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
    $ cd /pub
    $ more beer
    Army1987, Sep 5, 2007
    #2
    1. Advertising

  3. Cric

    SM Ryan Guest

    Cric <> wrote:
    # Hi,
    #
    # how can be the declaration of
    #
    # a pointer which points to dynamic array of pointers and each pointer
    # from this array points to an dynamic array of pointers which points to
    # character strings

    Also known as Iliffe vectors <http://www.google.com/search?q=iliffe+vector>.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Leave it to the Catholics to destroy existence.
    SM Ryan, Sep 5, 2007
    #3
  4. Cric

    Cric Guest

    Sorry may be my description is not clear

    I want data storage as
    ------------------------
    <first block>

    <char string>
    <char string>
    <char string>
    <char string>
    ....
    ....<dynamic>
    ------------------------
    <second block>

    <char string>
    <char string>
    <char string>
    <char string>
    ....
    ....<dynamic>
    --------------------------
    <Third block>

    <char string>
    <char string>
    <char string>
    <char string>
    ....
    ....<dynamic>
    ----------------------------
    many more dynamic blocks may be addedd

    so how to have this kind of data structure with reference to pointers

    Thanks

    Cric
    Cric, Sep 5, 2007
    #4
  5. On Wed, 05 Sep 2007 05:01:12 -0700, Cric <> wrote:

    >Sorry may be my description is not clear
    >
    >I want data storage as
    >------------------------
    ><first block>
    >
    ><char string>
    ><char string>
    ><char string>
    ><char string>
    >...
    >...<dynamic>
    >------------------------


    Are all of the multiple strings the same size (or the same maximum
    size)? Equivalently, given the address of the first string, can I
    compute the address of the second without evaluating the actual length
    of the string?

    If the answer is yes, then you can point to this block of strings
    (actually an array of strings) with a pointer to the first string.
    char (*p1)[MAX_STRING_SIZE] = malloc(N * sizeof *p1); would allocate
    space for N such strings, each occupying an array of MAX_STRING_SIZE.
    p1[0] would be the first array, p1[1] the second, etc. p1 can be
    reallocated to allow more strings.

    If you do this, you need some way to tell when you are at the last
    p.

    You either keep track of the maximum i p is valid for
    elsewhere or

    You set the last string to a sentinel value, such as
    strcpy(p1[N-1],"");

    If the strings are not the same size (often called a jagged or ragged
    array), then you basically need one pointer for each string. Since
    the string sizes vary, the simplest approach is a char* that points to
    the start of the string. The obvious solution is a dynamic array of
    such pointers. Something along the lines of

    char **p2 = malloc(N * sizeof *p2);
    p2[0] = &string0;
    p2[1] = &string1; ...

    You have the same choices as above for determining the number or
    strings plus the option of setting p2[N-1] to NULL. p2 can be
    reallocated to allow more strings. Furthermore, if any string is also
    dynamically allocated, p2 can be reallocated as well.

    snip second block

    ><Third block>
    >
    ><char string>
    ><char string>
    ><char string>
    ><char string>
    >...
    >...<dynamic>
    >----------------------------
    >many more dynamic blocks may be addedd
    >


    Now you have a dynamic number of blocks. You want a dynamic array
    where each element is a pointer to a block.

    If you chose the p1 approach above, then the pointer to the block has
    type char(*)[MAX_STRING_SIZE]. The simplest approach is to change the
    definition of p1 to

    typedef char (*T1)[MAX_STRING_SIZE];
    T1 p1 = malloc ...;

    and your pointer for allocating a dynamic array of T1's is

    T1 *da1 = malloc(M * sizeof *da1);

    On the other hand, if you are a glutton for punishment, you can omit
    the typedef and code

    char (*da1)(*)[MAX_STRING_SIZE] = malloc ...

    If you chose the p2 approach above, the declarations are much simpler.

    char ***da2 = malloc(M * sizeof *da2);


    Remove del for email
    Barry Schwarz, Sep 6, 2007
    #5
  6. Cric

    Martin Wells Guest

    Cric:

    > how can be the declaration of
    >
    > a pointer which points to dynamic array of pointers and each pointer
    > from this array points to an dynamic array of pointers which points to
    > character strings



    Use typedef's if you're brain hurts. Then try to wean yourself off
    them.

    Work backwards, reading from right to left:

    1: character strings = arrays of char = char arr[NUM_1];
    2: dynamic array of pointers which points to = char **p = malloc(NUM_2
    * sizeof(char*));
    3: a pointer which points to a dynamic array of pointer and each
    pointer from this array points to:
    char ***p = malloc(NUM_3 * sizeof(char**));

    Martin
    Martin Wells, Sep 10, 2007
    #6
  7. Cric

    pete Guest

    Cric wrote:
    >
    > Hi,
    >
    > how can be the declaration of
    >
    > a pointer which points to dynamic array of pointers and each pointer
    > from this array points to an dynamic array of pointers which points to
    > character strings
    >
    > p-->p-->p
    > p
    > p-->p
    > p
    > p
    > p-->p
    > p
    > p
    > p
    >
    > How to use such storage to fill the strings and then retrieve. Does
    > anybody has the example
    > upto this deep pointing algorithm.


    How do you feel about using a list of lists of pointers to strings
    instead?

    --
    pete
    pete, Sep 11, 2007
    #7
    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. news.amnet.net.au
    Replies:
    1
    Views:
    554
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    718
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    669
    Robert Mark Bram
    Feb 4, 2007
  4. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    729
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    437
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page