length of 2D Array >> char **myString= (char **) malloc (sizeof (char *));

Discussion in 'C++' started by davidb, Sep 1, 2006.

  1. davidb

    davidb Guest

    Hi,
    does someone know how to get the length of a 2 dimensional string
    array:
    here what i need:

    ----------------------------------------------------------------

    char **getList(void){

    char **myString= (char **) malloc (sizeof (char *));

    for(int i=0;i<10;i++){
    myString= (char **) realloc (myString, (i+1) * sizeof (char *));
    myString = (char *) malloc (255 * sizeof(char));
    strcpy (myString,"List Item");
    }
    return myString;
    }

    void something(void){
    char **dataList = getList();

    int length = ????? // IDEA ?

    for(int i=0;i<length;i++){
    printf(dataList);
    }
    }

    ----------------------------------------------------------------

    i've done several experiments with sizeof like
    int length = (sizeof(array)/sizeof(array[0])
    but most time i always get length = 1.

    sorry, thats a bit simple, but i am cosseted java progger ;)
    you know > "my String".length.
    So, if you know, please tell me.

    best Regards,
    David
    davidb, Sep 1, 2006
    #1
    1. Advertising

  2. davidb posted:

    > Hi,
    > does someone know how to get the length of a 2 dimensional string
    > array:



    It's not possible using a pointer alone.

    If you're going to be using a pointer (e.g. with dynamic allocation), then
    you'll have to keep track of the length yourself -- Standard C++ doesn't
    provide such a facility. Even something as simple as:

    #include <cstddef>

    template<class T>
    struct ArrPtr {
    T *p;
    size_t len;
    };

    --

    Frederick Gotham
    Frederick Gotham, Sep 1, 2006
    #2
    1. Advertising

  3. davidb

    davidb Guest

    Hi,
    yes, thats great, i dont thought about returning all that values in one
    struct.
    Sometimes it is that easy ;)

    Thank you,
    David

    Frederick Gotham schrieb:

    > davidb posted:
    >
    > > Hi,
    > > does someone know how to get the length of a 2 dimensional string
    > > array:

    >
    >
    > It's not possible using a pointer alone.
    >
    > If you're going to be using a pointer (e.g. with dynamic allocation), then
    > you'll have to keep track of the length yourself -- Standard C++ doesn't
    > provide such a facility. Even something as simple as:
    >
    > #include <cstddef>
    >
    > template<class T>
    > struct ArrPtr {
    > T *p;
    > size_t len;
    > };
    >
    > --
    >
    > Frederick Gotham
    davidb, Sep 1, 2006
    #3
  4. Frederick Gotham posted:

    > size_t len;



    std::size_t len;

    --

    Frederick Gotham
    Frederick Gotham, Sep 1, 2006
    #4
  5. Re: length of 2D Array >> char **myString= (char **) malloc (sizeof(char *));

    davidb schrieb:
    > Hi,
    > does someone know how to get the length of a 2 dimensional string
    > array:


    It is a one dimensional C-style-string array. Or a 2 dimensional char array.

    > ----------------------------------------------------------------
    >
    > char **getList(void){
    >
    > char **myString= (char **) malloc (sizeof (char *));
    >
    > for(int i=0;i<10;i++){
    > myString= (char **) realloc (myString, (i+1) * sizeof (char *));
    > myString = (char *) malloc (255 * sizeof(char));
    > strcpy (myString,"List Item");
    > }
    > return myString;
    > }

    [...]
    > ----------------------------------------------------------------
    >
    > i've done several experiments with sizeof like
    > int length = (sizeof(array)/sizeof(array[0])
    > but most time i always get length = 1.
    >
    > sorry, thats a bit simple, but i am cosseted java progger ;)
    > you know > "my String".length.
    > So, if you know, please tell me.


    Why don't you use standard C++ classes?

    #include <iostream>
    #include <string>
    #include <vector>

    int main()
    {
    std::vector<std::string> myList;
    list.push_back("Item 1");
    list.push_back("Item 2");

    int length = myList.size();

    for (int i = 0; i < length; i++)
    std::cout << myList << std::endl;

    return 0;
    }

    --
    Thomas
    Thomas J. Gritzan, Sep 1, 2006
    #5
  6. davidb

    Jerry Coffin Guest

    In article <ed9n2r$cfa$>,
    says...

    [ ... ]

    > int length = myList.size();
    >
    > for (int i = 0; i < length; i++)
    > std::cout << myList << std::endl;


    std::copy(myList.begin(), myList.end(),
    std::eek:stream_iterator(std::cout, "\n"));

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 1, 2006
    #6
  7. davidb

    Default User Guest

    davidb wrote:

    > Hi,



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>




    Brian
    Default User, Sep 1, 2006
    #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. Luis

    MyString Class

    Luis, Oct 24, 2003, in forum: C++
    Replies:
    4
    Views:
    2,042
    Samuele Armondi
    Oct 24, 2003
  2. jstanforth
    Replies:
    13
    Views:
    12,736
  3. davidb
    Replies:
    0
    Views:
    761
    davidb
    Sep 1, 2006
  4. fuzhen
    Replies:
    8
    Views:
    531
    rahul
    Jun 12, 2008
  5. Replies:
    46
    Views:
    652
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page