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

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

Post by david » Sun, 03 Sep 2006 00:22:32


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]);
}
}

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

if 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
 
 
 

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

Post by david » Sun, 03 Sep 2006 00:22:46

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

 
 
 

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

Post by Frederick » Sun, 03 Sep 2006 00:39:04

davidb posted:



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
 
 
 

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

Post by david » Sun, 03 Sep 2006 00:44:40

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:
 
 
 

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

Post by Frederick » Sun, 03 Sep 2006 01:14:29

Frederick Gotham posted:



std::size_t len;

--

Frederick Gotham
 
 
 

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

Post by Thomas J. » Sun, 03 Sep 2006 01:28:08

davidb schrieb:

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

[...]

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
 
 
 

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

Post by Jerry Coff » Sun, 03 Sep 2006 02:07:47

In article <ed9n2r$cfa$ XXXX@XXXXX.COM >,
XXXX@XXXXX.COM says...

[ ... ]


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

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
 
 

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

Post by Default Us » Sun, 03 Sep 2006 02:57:30


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.yqcomputer.com/ ++-faq-lite/how-to-post.html>




Brian