
October 23rd, 2004
01:04 AM
Neverside Newbie
Status: Offline!
C++ library that uses charAt()?
Hey, can somebody tell me if C++ has a function called charAt() in any of it's libraries? I'm using Visual Studio.NET Professional 2003 if that helps.
If it doesn't, can somebody give me a rough guideline as to how do program the function myself, or even just give me it? 

October 23rd, 2004
02:37 AM
Neverside Newbie
Status: Offline!
doesnt charAt just loop till it finds index? if so, why not something like this?
char charAt(char *somestring,int count) {
int i;
for (i = 0; i < count; i++) {
somestring++;
}
return *somestring;
}

October 23rd, 2004
04:01 AM
without any checking to make sure it won't fail, its actually easier than that. pos is the offset into the string, 0 will return first character.
char charAt(char *str, int pos)
{
return(str[pos]);
}
___________________
--Life would make more sense if I had the source code--

October 23rd, 2004
05:40 AM
Neverside Newbie
Status: Offline!
wow, that is possible, such a simple thing yet i never picked up on that...

October 23rd, 2004
10:43 PM
Neverside Newbie
Status: Offline!
Hey, thanks the principle behind that worked.
Incidentally, when I did that it didn't really work, it gave me errors about converting string to char which I half expected, but even just:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name;
name="abcdefg";
cout << name[3];
return 0;
}
hopefully returns "d".
I just didn't realise that a string was an array of characters that you could access easily, so it's pretty easy to build a function out of it. Thanks a lot! 

October 23rd, 2004
10:48 PM
Neverside Newbie
Status: Offline!
Also, since i've opened this thread, can somebody tell me what *somestring means?
Like both of you did in your functions, you used an asterisk before the string variable, what does that mean?
I know & is reference, but I haven't come across this yet.

October 25th, 2004
11:15 AM
WHAT HAVE YOU DONE
Status: Offline!
That is the way you reference the data pointed to by a pointer.
___________________
Sykil |
| UT or die

October 25th, 2004
09:51 PM
well, my functions would of worked if name was a char array:
char name[32];
or
char name[] = "billybob";
you could also do:
char *name;
name = (char *)calloc(32, sizeof(char));
sprintf(name, "billybob");
string is just a wrapper for char arrays but does all the resizing for you. you could of probably passed it in as (char *)name by casting it.
___________________
--Life would make more sense if I had the source code--

October 25th, 2004
09:53 PM
int *i; pointer to an int
char *c; pointer to a char.
pointers are just how they sound. They point to the very first byte allocated for a variable/object/etc in memory. If you were to print out &c or &i it would print out that address in memory. *i or *c just says, use the values that i'm pointing at in memory.
___________________
--Life would make more sense if I had the source code--