Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > length of 2D Array >> char **myString= (char **) malloc (sizeof (char *));

Reply
Thread Tools

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

 
 
davidb
Guest
Posts: n/a
 
      09-01-2006
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[i] = (char *) malloc (255 * sizeof(char));
strcpy (myString[i],"List Item");
}
return myString;
}

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

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

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

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

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

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      09-01-2006
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
 
Reply With Quote
 
 
 
 
davidb
Guest
Posts: n/a
 
      09-01-2006
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


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-01-2006
Frederick Gotham posted:

> size_t len;



std::size_t len;

--

Frederick Gotham
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      09-01-2006
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[i] = (char *) malloc (255 * sizeof(char));
> strcpy (myString[i],"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[i] << std::endl;

return 0;
}

--
Thomas
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-01-2006
In article <ed9n2r$cfa$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

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


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

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-01-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments