473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

char** question...

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

Aug 6 '06 #1
15 1790
* 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. :-)

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 6 '06 #2
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

Aug 7 '06 #3
* 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**.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '06 #4

RAB wrote:
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;

Aug 7 '06 #5
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';
}
}

--

Frederick Gotham
Aug 7 '06 #6
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

Aug 7 '06 #7
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

Aug 7 '06 #8
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

Aug 7 '06 #9
* 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?
char* pointers[300];

>Initialize the pointers to point to the strings.
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.

I am a hobbiest programmer and char arrays have always been a challenge
for me.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '06 #10
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.

--

Frederick Gotham
Aug 7 '06 #11
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.

--

Frederick Gotham
Aug 7 '06 #12

"RAB" <ra*********@yahoo.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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

Aug 7 '06 #13
RAB schrieb:
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);
}

--
Thomas
Aug 7 '06 #14
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[i]=(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

Aug 8 '06 #15
RAB
I am sorry, the for-loop should be

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

Thanks,
RABMissouri

Aug 8 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nicholas Parnell | last post by:
Hi! I have the problem where I cast a bit string of "10000000" to a byte, which i get -128; this is fine so far. However, when I take this byte and cast it to a char, I get a question mark('?')....
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
1
by: b83503104 | last post by:
When are they not consistent?
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
7
by: owolablo | last post by:
Can anybody please tell me how to change the individual elements of a char variable. I need to parse through the string, check for a particular character and change it to something else if it is...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
12
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
16
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.