• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can anyone help me with C++, returning a character array from a function.

bigshooter

Platinum Member
I have a program where i input a character array in main, then i pass that to a function, create a new array with a code based on the old array, and then return the new array to main so that it can be output. I'm getting "return to `char' from `char *' lacks a cast" when i compile. I know cast has something to do with pointers, and an array is basically a pointer to a chunk of memory, but i'm just now getting back into c++ and can't figure this out. If anyone can help I can email you the source code, or send it to you over some type of IM, because posting it here would be too long. THanks.
 
Why not use a string instead of a char array?

If you do have to pass a char array, pass it as a pointer. e.g.

int main()
{
char buf[] = "test";
char *p;
p = function(buf);
cout << "p = " << p;
delete p;
}

char *function( char *buf )
{
char *q;
q = new char[strlen(buf)+1];
strcpy(q,p);
return q;
}
 
it won't let me pm this so here it is


#include <iostream.h>
#include <cctype>
char soundex (char []); //soundex conversion program
static const int namesize = 40; // size of name array

int main ()
{
cout << "Enter up to 10 names " << endl;
char inputword[namesize];
for (int i=0; i<10; ++i) //runs program 10 times
{
cin >> inputword;
char foo;
foo=soundex(inputword);
cout << inputword << endl;
}
return 0;
}


char soundex (char word[]) //soundex conversion fucntion
{
char deadbeef[namesize];//temp array
strcpy( deadbeef, word );
for (int i=0; deadbeef!='\0'; ++i)//converts inputname to upper case
deadbeef = toupper(deadbeef);

char foo[6]="Z0000"; //soundex code array initialized to 0 for padding

if( isalpha( deadbeef[0] ) )
{
foo[0]=deadbeef[0];
//makes first letter of name first char of code
}

int holder=1; //where in soundex code prog is
char code; //what current code is
char first; //used for first part of code and for comparison
//go through input until word is
for (int j=0; deadbeef[j]!='\0' && holder<6; ++j)
{
//finished or until soundex(foo) is full
switch(deadbeef[j])
{
case 'B':
case 'P':
case 'F':
case 'V':
code='1';
break;

case 'C':
case 'S':
case 'K':
case 'G':
case 'J':
case 'Q':
case 'X':
case 'Z':
code='2';
break;

case 'D':
case 'T':
code='3';
break;

case 'L':
code='4';
break;

case 'M':
case 'N':
code='5';
break;

case 'R':
code='6';
break;

default:
code='0';
break;
}
if (j==0)
{
first=code;
}
else
{
if ((code!=first)&& ( code != '0' ))
{
foo[holder]=code;
first=code;
++holder;
}
}
}
return foo;

}


 
Return a char pointer from your function.
Internally within your function, return the pointer to the array you have built.
 
Back
Top