Anyone know any C++?

Recommended Videos

AzzA-D

Regular Member
Nov 18, 2009
46
0
11
Hi guys,
I hate having to ask, because its probably a fairly noobish question, but Im having a problem with char arrays and passing them between functions, and its driving me crazy.

Basically Im asking the user for an input string, creating a char array out of it, and then passing it back to my main function - however the output is always a random string of unicode characters like this - ╠╠╠╠╠╠╠╠↑

I have recreated the problem in something a bit less messy than my actual project file, so code is in the spoilers.

const int BUFFER = 50;

char* getWord()
{
char word[BUFFER];

cout << "Please enter a word: ";
cin.getline(word, BUFFER);
cout << word << "\n"; // Returns the input string no problem.

return word;
}

int main()
{
char* wordPtr;

wordPtr = getWord();
cout << "Your word is: " << wordPtr << "\n"; // Returns unicode characters. Why?

system("PAUSE");
return 0;
}
Can anyone explain why?

Any help would be greatly appreciated.
 

Esotera

New member
May 5, 2011
3,396
0
0
AzzA-D said:
Hi guys,
I hate having to ask, because its probably a fairly noobish question, but Im having a problem with char arrays and passing them between functions, and its driving me crazy.

Basically Im asking the user for an input string, creating a char array out of it, and then passing it back to my main function - however the output is always a random string of unicode characters like this - &#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#8593;

I have recreated the problem in something a bit less messy than my actual project file, so code is in the spoilers.

const int BUFFER = 50;

char* getWord()
{
char word[BUFFER];

cout << "Please enter a word: ";
cin.getline(word, BUFFER);
cout << word << "\n"; // Returns the input string no problem.

return word;
}

int main()
{
char* wordPtr;

wordPtr = getWord();
cout << "Your word is: " << wordPtr << "\n"; // Returns unicode characters. Why?

system("PAUSE");
return 0;
}
Can anyone explain why?

Any help would be greatly appreciated.
Unicode letters mean that you're probably returning some random memory address, and that value is converted to a letter in the console. In this case I'd say that you're probably returning the pointer to the array, but haven't looked through your code that much (use code tags next time).

As for arrays, I think the way to pass them is something like this (when they're unidimensional, like yours is):
Code:
int someArray[5];

someFunction(someArray)

Just a note on style, char* array is absolutely horrible, unless you specifically have to use one, use std::string instead. C++ isn't my first language but feel free to quote me if you have any other questions.
 

Hiikuro

We are SYD!
Apr 3, 2010
230
0
0
AzzA-D said:
Hi guys,
I hate having to ask, because its probably a fairly noobish question, but Im having a problem with char arrays and passing them between functions, and its driving me crazy.

Basically Im asking the user for an input string, creating a char array out of it, and then passing it back to my main function - however the output is always a random string of unicode characters like this - &#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#8593;

I have recreated the problem in something a bit less messy than my actual project file, so code is in the spoilers.

const int BUFFER = 50;

char* getWord()
{
char word[BUFFER];

cout << "Please enter a word: ";
cin.getline(word, BUFFER);
cout << word << "\n"; // Returns the input string no problem.

return word;
}

int main()
{
char* wordPtr;

wordPtr = getWord();
cout << "Your word is: " << wordPtr << "\n"; // Returns unicode characters. Why?

system("PAUSE");
return 0;
}
Can anyone explain why?

Any help would be greatly appreciated.
The array "word" in getWord() is local to the function, this means it essentially (to avoid the technical details of the stack) ceases to exist once the function ends. I think accessing a pointer to this array after the function has ended is undefined behaviour (as the pointer now points to invalid/"garbage" memory), and in this case it seems like some cleanup takes place after the function ends.

My initial suggestion is to instead use C++ style strings (std::string), as they manage memory themselves internally. Another suggestion, if you want to use char* arrays is to pass the pointer as an argument to the function, as this makes sure the variable is not out of scope:
Code:
void getWord(char *word)
{
   // Function is equivalent except there is no return statement or a char array definition
}

int main(void)
{
   // ...
   char buffer[BUFFER];
   getWord(buffer); // Stores the word in the buffer.
   // ...
}

// Or with dynamic memory:
int main(void)
{
   char *buffer = new char[BUFFER];
   getWord(buffer);

   // And at the very end, after everything relevant to "buffer" has been done, cleanup the memory.
   delete[] buffer;
}
Hope that was helpful.
 

AzzA-D

Regular Member
Nov 18, 2009
46
0
11
Cheers for your help guys, is really useful.
Sorry about not using code tags, I didnt realise they existed here.
Again, thanks for quick replies and for the help!