so I'm implementing this chess program on C++ and I'm trying to integrate to winboard protocol...one of the functions that they say I need to write to do so should have the following signature:
char *MoveToText(MOVE move); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
my question is....the text formats are something like e2e2....but the return type of that function is char...and as far as I can understand it, char is just one single character....
so how come are they telling me to use this signature?
or am I mistaken and in fact char can also store multiple characters such as e2e2, e1g1 etc?
Yeah, in C, a char* points to an array of characters. C treats arrays of characters as strings, terminated by a null byte.
The return is a char* or a c-style string =)
char * is a pointer on char - address of sequence of characters.
It returns pointer to char, which is basically a c-string.
Take a look at this tutorial: http://www.cprogramming.com/tutorial/lesson9.html
Related
I have to compare 2 strings, one is from a structure member and the other is "Empty".
I use strcmp like so:
if (strcmp(e[n]->seat[seat/5][(seat%5)-1], "Empty")==0)
I am getting the error Invalid conversion from char to const char*.
Can someone please help me out with this?
It sounds like e is an array of structures each containing a 3 deminsional character array. You should probably be using a two deminsional char pointer array unless you /know/ the string length won't change but if you don't just use the address like chnossos said.
if (strcmp(&(e[n]->seat[seat/5][(seat%5)-1]), "Empty")==0)
What is the difference between
char (CharBuff[50])[10];
and
char CharBuff[10][50];
My requirement is to have 10 character buffers, each of length 50 max (including null terminating character)
And, I would like to access the buffers as CharBuff[0], CharBuff[1] and so on.
char (CharBuff[50])[10];
declares CharBuff as a 50-element array of 10-element arrays of char. The parentheses are superfluous in this case.
char CharBuff[10][50];
declares CharBuff as a 10-element array of 50-element arrays of char.
Given that you want 10 strings of up to 50 characters, you would use the second form; the type of each CharBuff[i] will be "50-element array of char".
If you really wanted to create a separate type definition for a 50-element array of char, you could do something like
typedef char Str[50];
...
Str CharBuff[10];
Now CharBuff is a 10-element array of Str, which is a 50-element array of char.
Normally, I would not create a separate typedef like this unless I wanted to make Str opaque; that is, I don't want to expose the details of its implementation to whomever's using it. In addition to the typedef, I'd also supply an API for allocating, assigning, copying, formatting, and displaying objects of type Str.
Put another way, if the person using the Str type has to be aware that it's a 50-element array of char in order to use it properly, then it's better to just make them use a 50-element array of char.
The other answers here which say they're the same are wrong! Both are NOT the same arrays and their sizes are very different. Here's a snippet to show that:
char i[50][10];
std::cout << sizeof(i[1]) << '\n';
char (j[10])[50];
std::cout << sizeof(j[1]) << '\n';
10
50
You can see the live example here.
i is a 50-element array with each element being a 10-element array of characters, while j is a 10-element array with each element being a 50-element array of characters. Although the total sizes of both would be the same, the size of an element at each level would be different. If you assume they're the same, it would lead to undefined behaviour
i[25][5] // OK
j[25][5] // accessing j beyond index 9 is undefined behaviour!
This shows that the parenthesis have no significance in a non-pointer, non-reference array declaration i.e. char (j[10])[50] is just confusing notation for char j[10][50].
My requirement is to have 10 character buffers, each of length 50 max
Then you should declare your array as char CharBuff[10][50].
Nobody ever uses the former, always use the latter form:
char CharBuff[10][50];
Latter syntax will be proficient and less confusing as per you requirement of 10 rows (char buffers) having length of 50 each.
Go for the last one as it clearly states what you are allocating.
However, since the question is tagged with c++ too, I would advise using std::vector and std::string as it follows:
std::vector<std::string> CharBuff(10, std::string(50, '\0'));
The big problem that i see here is confusing syntax. When you use parenthesis they already have known roles in c and c++ syntax such as type conversions, pointers to functions(which looks a bit like what you wrote). What you are using them for add's a new meaning which makes code more obfuscated and ignores the fact that c and c++ have a great and intuitive way, for anybody that used matrices at least once, to express 2d arrays. So, for a clean and not confusing syntax use the latter version:
char CharBuff[10][50];
You are looking for 10 arrays of 50 chars each.
To declare a single buffer of 50 bytes, you would use
char CharBuff[50];
But you want to have 10 buffers, so just tack that on to before[50], e.g.:
char CharBuff[10][50];
Now CharBuff[0] will address the first fifty-byte buffer, CharBuff[1] will get the second, and so on.
I have a wide char variable which I want to initialize with a size of string.
I tried following but didn't worked.
std::string s = "aaaaaaaaaaaaaaaaaaaaa"; //this could be any length
const int Strl = s.length();
wchar_t wStr[Strl ]; // This throws error message as constant expression expected.
what option do i have to achieve this? will malloc work in this case?
Since this is C++, use new instead of malloc.
It doesn't work because C++ doesn't support VLA's. (variable-length arrays)
The size of the array must be a compile-time constant.
wchar_t* wStr = new wchar_t[Strl];
//free the memory
delete[] wStr;
First of all, you can't just copy a string to a wide character array - everything is going to go berserk on you.
A std::string is built with char, a std::wstring is built with wchar_t. Copying a string to a wchar_t[] is not going to work - you'll get gibberish back. Read up on UTF8 and UTF16 for more info.
That said, as Luchian says, VLAs can't be done in C++ and his heap allocation will do the trick.
However, I must ask why are you doing this? If you're using std::string you shouldn't (almost) ever need to use a character array. I assume you're trying to pass the string to a function that takes a character array/pointer as a parameter - do you know about the .c_str() function of a string that will return a pointer to the contents?
std::wstring ws;
ws.resize(s.length());
this will give you a wchar_t container that will serve the purpose , and be conceptually a variable length container. And try to stay away from C style arrays in C++ as much as possible, the standard containers fit the bill in every circumstance, including interfacing with C api libraries. If you need to convert your string from char to wchar_t , c++11 introduced some string conversion functions to convert from wchar_t to char, but Im not sure if they work the other way around.
So I have this:
char uloginName[] = "derp";
char mew[33] = "home/home/" << uloginName << "\0";
I am trying concatinate uloginName with the rest of the string that will later be converted to an array of char. But it keeps returning me an error. I don't know how to do it.
Also, I must use only char[] type as of this moment; No string.
Thank you for the help.
char uloginName[]="derp";
char mew[33]="home/home/";
strcat(mew, uloginName);
You can use strcat on arrays of characters, so long as there is sufficient space and they are terminated with a zero byte.
Use strncat().
It looks like you are looking for std::ostringstream, which is a versatile and far less error-prone way of handling strings in C++. strcat(), strncat and their kin are hangovers from C and should be used cautiously in C++.
char uloginName[] = "derp";
std::ostringstream mew;
mew << "home/home/" << uloginName;
I've a question concerning a conversion from char to TPtrC8 on Symbian. How can I transform a char to TPtrC8 or a TBuf to TPtrC8? Is there a simple and quick way to do that?
The TPtrC8 has constructors that take char* and TDes8 as arguements. This effectively wraps the TPtrC8 around the the character array in the char* or the TBuf8
TBuf is a 16 bit construct and you will have to convert it to a TBuf8 before you can use it with a TPtrC8. If you are using a single character literal, you also might have to put it into a TBuf8. TDes8::Copy is your friend.
Thanks and done by the following code:
TBuf<25> iIMSI; TPtrC iPtrC(iIMSI);
TPtrC8 iPtrC8((TUint8*)iPtrC.Ptr(), iPtrC.Length()*2);