Im new to c++, after trying to devlop in go I figured might as well learn c++ since the learning curve will be so steep the moment I start going down abstraction layers I'm use to handling dynamic arrays in php with ease however I cannot understand the pointers, and memory allocation, and scope issues associated with c++ heres my first attempt:
string* Deck() {
// array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands
string* Deck = new string[52] ;
Deck = {
"A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H",
"A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S",
"A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D",
"A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C",
};
return Deck;
}
int main()
{
string* Deck = Deck();
cout << *Deck[23] << endl;
return 0;
}
the compiler is giving me various errors I've tried simply returning the pointers but I found out about the scope in this article Return string array in C++ function
Theres many articles regarding int as datatype but strings and int=>string explanations are rare as I guess everyone assumes even a noob could figure things out but I have watched countless hours of youtube videos and still can't quite figure it out . Thanks.
cout << Deck[23] << endl;without the star: this would give cout a std::string which I think it can print without any further conversion. (I assume you didn't mean to dereference the pointer to get the first character only to print, as would have happened if this was an array of C not C++ strings?)Deck- particularly having both variables and a function called the same thing. Even if this works as-is (and I'm not sure it would) it would be clearer to a reader which Deck is which if you gave them different names.