Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

77 users online



C++ library that uses charAt()?

C++ library that uses charAt()?

Currently viewing this thread: 1 (0 members and 1 guests)


Page 1 out of 2
Avantasia

Avantasia

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? Grin

zium

zium

Neverside Newbie
Status: Offline!

doesnt charAt just loop till it finds index? if so, why not something like this?

Code:

char charAt(char *somestring,int count) {
int i;
for (i = 0; i < count; i++) {
somestring++;
}

return *somestring;
}

vek

vek

die();
Status: Offline!

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.

Code:


char charAt(char *str, int pos)
{
return(str[pos]);
}

___________________

--Life would make more sense if I had the source code--

zium

zium

Neverside Newbie
Status: Offline!

wow, that is possible, such a simple thing yet i never picked up on that...

Avantasia

Avantasia

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! Smile

Avantasia

Avantasia

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.

Avantasia

Avantasia

Neverside Newbie
Status: Offline!

Anybody?

Xaimoatse

Xaimoatse

WHAT HAVE YOU DONE
Status: Offline!

That is the way you reference the data pointed to by a pointer.

___________________

Sykil | http://www.dark-storm.com/n-unit.png | UT or die

vek

vek

die();
Status: Offline!

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--

vek

vek

die();
Status: Offline!

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--

Page 1 out of 2
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0077 seconds.