I’ve not used C++ very much in the past, and have recently been doing a lot of C#, and I’m really struggling to get back into the basics of C++ again. This is particularly tricky as work mandates that none of the most handy C++ constructs can be used, so all strings must be char *’s, and there is no provision for STL lists.
What I’m currently trying to do is to create a list of strings, something which would take me no time at all using STL or in C#. Basically I want to have a function such as:
char **registeredNames = new char*[numberOfNames];
Then,
RegisterName(const * char const name, const int length) { //loop to see if name already registered snipped if(notFound) { registeredNames[lastIndex++] = name; } }
or, if it was C#…
if(!registeredNames.Contains(name)) { registeredNames.Add(name); }
and I realize that it doesn’t work. I know the const nature of the passed variables (a const pointer and a const string) makes it rather difficult, but my basic problem is that I’ve always avoided this situation in the past by using STL lists etc. so I’ve never had to work around it!
You’ll probably need to use strcmp to see if the string is already stored:
Then if you really need to store a copy of the string, then you’ll need to allocate a buffer and copy the characters over.
You didn’t mention whether your input is NULL terminated – if not, then extra care is needed, and strcmp/strcpy won’t be suitable.