[try Beta version]
Not logged in

 
 
How to return a c string array?

Mar 30, 2016 at 10:23pm
How can I return a c string array? I'm writing a function that needs to return a c string array reference but I keep getting errors when I try to return it. How can I return my c string array properly? Everything in the function works properly but I just need to know how to return the cstring that I made. How would I do that? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  
char *encode(HuffmanCodec *h, const char message[])	{

	char * encoded[500];
	
	for (int i = 0; message[i] != '\0'; i++)	{
			
			encoded[i] = h->codebook[(int) message[i]];
			cout << "Code: " << i+1 << " is " << encoded[i] << endl; // used for testin,
		}
	
  return NULL; // ???????
  
}
Last edited on Mar 30, 2016 at 10:29pm
Mar 30, 2016 at 10:29pm
char * encoded[500];
This is an array of 500 char-pointers. Is that what you meant to create? Or did you mean to create an array of 500 chars?

If you want to return it, you'll need to make sure it continues to exist after the function finishes. This can be done by creating it using new. Be sure to delete it when you're done.

A much better option is to just use a proper C++ string.
Topic archived. No new replies allowed.