gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS Misc > Strings and Pointers

#175789 - MichaelMossman - Mon Feb 07, 2011 1:10 pm

I am going round and round in circles, trying to create a String and then work my way through it with a pointer.

Either string to char pointer has been deprecated or else conversion from constant char to char is not allowed.

Surely, it can't be this difficult just to be able to create a string (say "TestString") and then create a pointer ( of one type or another) and then go through the string, one character at a time.

Can somebody please get me out of this loop.

#175795 - Rexhunter99 - Tue Feb 08, 2011 3:00 am

I can see why sometimes, this is why most tutorials/books these days argue why not to use C-Style strings and will tell you to use the C++ STD strings.

if you want to create a cstring and go through it a character at a time, I can suggest a few ways. It all depends on your code style and preference though.

First of all you can dynamically allocate memory for your string like so:
Code:
char *myString = new char[strlen("Hello World!")+1];

Then you copy the string into the new dynamic array:
Code:
strcpy(myString, "Hello World!");

And to cycle through it (old-school 1990's style) use a while loop like so:
Code:
char *pChar = myString;
while(*pChar != 0)
{
printf("myString[%d] = \'%c\'\n", int(pChar - myString), *pChar);
pChar++;
}

And ALWAYS make sure you free the memory you just used:
Code:
delete [] myString;
myString = 0;


That's one way with a mix of C++ and C, the C++ is the new/delete operators (I prefer them over alloc() and free()) with C-Style string handling that I've seen in many mid 1990's game source-code.

Using the same allocation and copying of the string into the dynamic array, you can use an array index to get the character too using a for loop instead of the while loop I supplied:
Code:

for (int i=0; i<strlen(myString); i++)
{
printf("myString[%d] = \'%c\'\n", i, myString[i]);
}

You could even change the for line to:
Code:
for (int i=0; myString[i]!=0; i++)

Which breaks out of the loop if we reach a NULL terminator in the string (assuming you have been a good programmer and have NULL terminated your string which is good practice anyway.)

As for const char* type casting, I have troubles with that too, then again I changed my way of using const, now I only use it when the value it is referring to WONT change (which is exactly what it means) I think if you declare your string as a constant character pointer, the compiler detects the type cast to a normal character pointer and immediately throws an error because you've told it that the values it contains wont be changed. (which is a good thing, that it's catching that one out for you)

I only use const char* in functions involving strings such as:
Code:
int find_substring(const char* string, const char* substring);

Where neither string or substring will be modified because I'm only looking at the data in either of them within the function.

Another way to do it is for instance:
Code:

char *myString = "Hello World!";

Though I don't quite trust that, I'd prefer to allocate memory dynamically for the string and handle the freeing of that memory myself.
Or this is valid too when initializing the variables if I remember correctly:
Code:

char myString[] = "Hello World!";
or
char myString[256] = "Hello World";


Strings are tricky business if not thought about carefully, pointers are not as tricky as they seem when you read a good article on them.
_________________
[Images not permitted - Click here to view it] - Move over Mario... Cruise by Crash... Croc Rocks!


Last edited by Rexhunter99 on Wed Feb 09, 2011 5:01 am; edited 1 time in total

#175799 - MichaelMossman - Tue Feb 08, 2011 4:03 pm

Thank you very much for that very lucid insight to the vagaries of string and char.

I am grateful for your comments and it has certainly helped me to solve my own particular problem, which was how to pass a string to a function and then let that function work on the string using char pointers.

What you have said actually clarifies the situation much more than most of the tutorials I have seen on the subject. For my own part, I managed to get round the apparent compiler impasse by actually using a string declaration in the function and passing a string into it. For some reason, I had convinced myself that the only way to pass a string was by using char pointers and that was the route cause of my problem.

#175800 - elhobbs - Tue Feb 08, 2011 7:27 pm

Rexhunter99 wrote:
First of all you can dynamically allocate memory for your string like so:
Code:
char *myString = new char[strlen("Hello World!")];

Then you copy the string into the new dynamic array:
Code:
strcpy(myString, "Hello World!");
this code creates a 1 character buffer overflow. it neglects to allocate space for the terminating null character that is expected for c strings. strlen does not count the terminating null character - you need to do it yourself. strcpy does copy the terminating null character - in this case past the end of the array you allocated.
I stopped reading at that point - there may or may not be more issues...

#175801 - Rexhunter99 - Wed Feb 09, 2011 4:59 am

elhobbs wrote:
Rexhunter99 wrote:
First of all you can dynamically allocate memory for your string like so:
Code:
char *myString = new char[strlen("Hello World!")];

Then you copy the string into the new dynamic array:
Code:
strcpy(myString, "Hello World!");
this code creates a 1 character buffer overflow. it neglects to allocate space for the terminating null character that is expected for c strings. strlen does not count the terminating null character - you need to do it yourself. strcpy does copy the terminating null character - in this case past the end of the array you allocated.
I stopped reading at that point - there may or may not be more issues...

Oh crud! You're right D= I totally forgot that because I was naughty and didn't check the reference to be extra sure D= *is crushed by noobness*

Anyways edited the post and fixed it all =) I think tutorials are correct though, C++ strings are the way to go now and IIRC libnds has C++ strings =D

Thanks for that catch elhobbs =)
_________________
[Images not permitted - Click here to view it] - Move over Mario... Cruise by Crash... Croc Rocks!

#175805 - Dwedit - Wed Feb 09, 2011 2:45 pm

#1: post should be moved out of "DS Misc"
#2: C++ strings are in the Standard Template Library, and are defined the standard library's header files, plus whatever needs to be linked from libcpp. Libnds isn't involved anywhere.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#175806 - Miked0801 - Wed Feb 09, 2011 7:21 pm

strncpy instead of strcpy whenever possible as well. Might as well not accidently blow your receive buffer.

Or just use the proper C++ methods and not worry about dynamic memory loss :)