Help!! How to return a pointer to beginning of string??
I'm sorry- but this is for an assignment and I am stuck.. I am trying to convert a string into uppercase..and then return a pointer to the beginning of the string in main..and then somehow copy the pointer into a string again.. when I try to compile I get the following error message..
error C2440: '=' : cannot convert from 'char *' to 'char [35]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
I'm not going to post the entire program because it's lengthy...and am hoping that somene could point me in the right direction from the pieces below..
Any suggestions or help would be greatly appreciated!!
lms
Code:
//function prototype
char* toUpperString(char[]);
//function call from main
char nameString[35];
cout<<"Enter name: ";
cin.getline(nameString,35);
nameString=toUpperString(nameString);
strncpy(e->name, nameString, 35);
//function to convert to uppercase..
char* toUpperString(char x[])
{
int length;
char* string;
for (int i=0; i<length; i++)
{
string[i]=toupper(x[i]);
}
return string;
}
Re: Help!! How to return a pointer to beginning of string??
Quote:
Originally posted by lms Code:
//function prototype
char* toUpperString(char[]);
//function call from main
char nameString[35];
cout<<"Enter name: ";
cin.getline(nameString,35);
nameString=toUpperString(nameString);
strncpy(e->name, nameString, 35);
//function to convert to uppercase..
char* toUpperString(char x[])
{
int length;
char* string;
for (int i=0; i<length; i++)
{
string[i]=toupper(x[i]);
}
return string;
}
well, you can do this:
Code:
>>char* toUpperString(char[]);
char toUpperString(char*x);
-edited version of toUpperString:
char toUpperString(char *x)
{
int length;
char* string;
for (int i=0; i<length; i++)
{
string[i]=toupper(x[i]);
}
return string;
}
question: do you want to make a copy of the string?
if not, you can save memory by removing that extra pointer.
Regards,
toaster