char** question...

R

RAB

I am programming in the palm environment using C++ and am stuck. I
have created an char array:

const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string50"}
}

I need to build a function that will return MyArray in the form of a
char**

I need to do it this way because of memory issues.

If any looping needs to be done I would need it done with the code that
is calling the function.

Any help would be appreciated.

Thanks,
RABMissouri
 
A

Alf P. Steinbach

* RAB:
I am programming in the palm environment using C++ and am stuck. I
have created an char array:

const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string50"}
}

Missing semicolon. Also, what you have here is three hundred strings of
max length 49, not fifty strings of max length 299. I never recall the
order because it's not something one would ordinarily use, but given a
piece of code with 2D array it's easy to check using a compiler.

I need to build a function that will return MyArray in the form of a
char**

You have a two-dimensional array, that's not the same as an array of
pointers.

One of the three requirements has to yield:

* Change the declaration of MyArray to an array of pointers.

* Change the declaration of the function to return a 'char const
(*)[300]' (or fifty, whatever was the intention).

* Discard the requirement that the function result should be MyArray
directly.

Assuming that you don't have an awful lot of memory to play around with
it seems the first point above is the best course of action, i.e.

static char const* const myArray[] =
{
"string1",
...
};
static size_t const myArray_length = sizeof(myArray)/sizeof(*myArray);


I need to do it this way because of memory issues.

Oh, yes. :)
 
R

RAB

Alf,

Sorry if I offended you. It was not my intention. I am just looking
for a solution.

Have you ever programmed in the palm environment? If you havent one
has 64k of ram and that is it. If one wants to store strings of data
beyond 64k one has to store exactly in the form I previously indicated.


const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string300"}
};

The tricky part is I need to convert MyArray to a char** so I can put
it in a listbox.

So the question still remains, how do I convert MyArray to a char**?

Thanks,
RABMissouri
 
A

Alf P. Steinbach

* RAB:
Alf,

Sorry if I offended you. It was not my intention. I am just looking
for a solution.
Huh?


Have you ever programmed in the palm environment?
Nope.


If you havent one
has 64k of ram and that is it. If one wants to store strings of data
beyond 64k one has to store exactly in the form I previously indicated.

I find neither statement easy to believe... OK, 64k might be
believable, on some old late 80's early 90's palmtop. But the second
statement contradicts that, and no computer or OS I have ever heard of
places requirements on the C or C++ data layout in order to be able to
use memory -- I am quite sure you have misunderstood something.

const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string300"}
};

The tricky part is I need to convert MyArray to a char** so I can put
it in a listbox.

So the question still remains, how do I convert MyArray to a char**?

Allocate an array A of 300 pointers. Initialize the pointers to point
to the strings. The address of the first element of A is your char**.
 
S

sun1991

RAB said:
Alf,

Sorry if I offended you. It was not my intention. I am just looking
for a solution.

Have you ever programmed in the palm environment? If you havent one
has 64k of ram and that is it. If one wants to store strings of data
beyond 64k one has to store exactly in the form I previously indicated.


const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string300"}
};

The tricky part is I need to convert MyArray to a char** so I can put
it in a listbox.

So the question still remains, how do I convert MyArray to a char**?

I think this maybe what you want:

char (*ptr)[50] = MyArray;
 
F

Frederick Gotham

RAB posted:
I need to build a function that will return MyArray in the form of a
char**


Here's a little sample code I cooked up with might be of help:

#include <iostream>
using std::cout;

unsigned const quantity_names = 50;

char const *const *GetNames()
{
/* Returns a null-terminated
array of pointers to strings. */

char const static *const arr[quantity_names] = {
"Adam", "Paul", "Philip", "James", "Matthew",
"Brian", "Thomas", "Mary", "Sandra", "Kelly",
"Lisa", "Heather" };

return arr;
}

int main()
{
for(char const *const *p = GetNames(); *p; ++p)
{
cout << *p << '\n';
}
}
 
R

RAB

I find neither statement easy to believe... OK, 64k might be
believable, on some old late 80's early 90's palmtop. But the second
statement contradicts that, and no computer or OS I have ever heard of
places requirements on the C or C++ data layout in order to be able to
use memory -- I am quite sure you have misunderstood something.

Belive it or not, the Palm OS uses 16 bit addresses, it is quite
archaic. It makes programming a challenge. It makes one appreciate
CStrings in the Microsoft architecture.
Allocate an array A of 300 pointers.
Could you share some code please?
>Initialize the pointers to point to the strings.
Could you share some code please?

I am a hobbiest programmer and char arrays have always been a challenge
for me.

Thanks,
RABMissouri
 
R

RAB

Hello Sun,

char (*ptr)[50];
compiles ok
char (*ptr)[50]=MyArray;
compiler error "illegal implicit conversion from 'const
char[300][50]' to 'char (*)[50]'

Also, inorder to copy char* one must use
StrCopy(Char *dst, const Char * src); //function definition

Thanks,
RABMissouri
 
R

RAB

Hi Frederick,

Thanks for you help! Your code looks great.

MyArray has already been created. So I need to create a char** from
MyArray.

Another thing I should have told you, is inorder to copy arrays I need
to use the function
StrCopy( Char *dst, const Char *src);

Thanks,
RABMissouri
 
A

Alf P. Steinbach

* RAB:
Belive it or not, the Palm OS uses 16 bit addresses, it is quite
archaic. It makes programming a challenge. It makes one appreciate
CStrings in the Microsoft architecture.
?


Could you share some code please?

char* pointers[300];

Could you share some code please?

No, this smells like homework. Use a loop. If you have any problems,
post your code and describe exactly what the problem is.
 
F

Frederick Gotham

RAB posted:
Hi Frederick,

Thanks for you help! Your code looks great.

MyArray has already been created. So I need to create a char** from
MyArray.

Another thing I should have told you, is inorder to copy arrays I need
to use the function
StrCopy( Char *dst, const Char *src);


If you want to make a copy of GetName's static array, then try something
like the following. (Note that the char*'s stored in the array must refer
to strings of static duration.)

template<class T>
unsigned LenNullTermArr(T const *const pstart)
{
T const *p = pstart;

while(*p++);

return (unsigned)(p - pstart) - 1;
/* Cast used to suppress warning */
}

#include <iostream>
using std::cout;

#include <cstdlib>
using std::memcpy;

unsigned const quantity_names = 50;

char const *const *GetNames()
{
/* Returns a null-terminated
array of pointers to strings. */

char const static *const arr[quantity_names] = {
"Adam", "Paul", "Philip", "James", "Matthew",
"Brian", "Thomas", "Mary", "Sandra", "Kelly",
"Lisa", "Heather" };

return arr;
}

int main()
{
char const *const *const pstatic = GetNames();

unsigned const len = LenNullTermArr(pstatic);

char **const pbuf = new char*[len + 1];

memcpy(pbuf,pstatic,(len + 1) * sizeof *pbuf);

/* Now "pbuf" points to the array copy. */

for(char const *const *p = pbuf; *p; ++p)
{
cout << *p << '\n';
}

delete [] pbuf;
}

It would be more complicated if you wanted to copy the actual strings
aswell.
 
F

Frederick Gotham

RAB posted:
I am a hobbiest programmer and char arrays have always been a challenge
for me.


Then practise.

I'm a hobbiest programmer and I've no problem with arrays of pointers to
arrays of pointers to arrays of char's.
 
H

Howard

RAB said:
Hello Sun,

char (*ptr)[50];
compiles ok
char (*ptr)[50]=MyArray;
compiler error "illegal implicit conversion from 'const
char[300][50]' to 'char (*)[50]'

Also, inorder to copy char* one must use
StrCopy(Char *dst, const Char * src); //function definition

That's not a standard C++ function, and the type Char is not a standard C++
type. It looks like you're using MFC or something. Why do you HAVE to use
that function? Why not strcpy? And what does the type Char have to do with
anything?

You need to be more clear on what this function you want is supposed to do.
You say it needs to use the function above to copy the data. Copy it from
what to what? And why does the loop you talked about have to be outside the
function? What exactly do you want to accomplish?

I'll try a guess, and some pseudo-code. Suppose you have an array like you
described. Now, you want to create a whole new array (but why, I don't
know), with copies of all the strings in the original array. Here's an
algorithm:

determine the number of strings in the original array
allocate that many pointers-to-char (to a pointer-to-pointer-to-char)
for every string in the original array
determine the size of the string there
add one to account for a null-terminator (assuming there are
null-terminated strings)
allocate an array of that size (to the pointer-to-char at this location)
copy the string from the original array to the newly allocated pointer
return the pointer-to-pointer-to-char

(And don't forget that with this method, you'll need to loop through the
array and delete[] each pointer-to-char, as well as delete[] the
pointer-to-pointer-to-char afterwards.)

Now, given this (assuming it's what you need), put it into C++ code. Let us
know if there's a problem with the code that you can't resolve.

-Howard
 
T

Thomas J. Gritzan

RAB said:
I am programming in the palm environment using C++ and am stuck. I
have created an char array:

const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string50"}
}

I need to build a function that will return MyArray in the form of a
char**

#include <algorithm>

const char MyArray[][50] =
{
{"string1"},
{"string2"},
{"string50"}
};

void DoSomething(const char**) {}

int main()
{
const char* MyTemp[sizeof(MyArray)/sizeof(*MyArray)];
std::copy(MyArray, MyArray+sizeof(MyArray)/sizeof(*MyArray), MyTemp);

DoSomething(MyTemp);
}
 
R

RAB

I came up with a solution. No compiler errors and no runtime errors

//I need to set up this array to store char strings beyond 64k of RAM
//It is located in what palm os calls a 'segment'
#pragma pcrelconstdata on
const char MyArray [300][50] =
{
{"string1"},
{"string2"},
....
{"string300"}
};
#pragma pcrelconstdata off

const char* BGReturn (int Num)
{
return BG[Num];
}

char* hold[300] = {
"0",
"1",
...
"300"
};

int i=0;
for(i=0; i<300; i++)
test=(char*) BGReturn(i); //thought I needed to use StrCopy but
I didn't

//I can then insert 'hold' into the listbox function that requires a
char**

This may not be the most eloquent solution but it works.

Thanks,
RABMissouri
 
R

RAB

I am sorry, the for-loop should be

int i=0;
for(i=0; i<300; i++)
hold=(char*) BGReturn(i); //thought I needed to use StrCopy but
//I didn't

Thanks,
RABMissouri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top