HOME       >>       Programming

CharAt For C?


Hey guys,

I'm just wondering if there's a CharAt function like there is in Java?

I know in Java, you just do this:

intArray[0][intCount] = strSIN.charAt(intCount) - '0';



Hey guys,
I'm just wondering if there's a CharAt function like there is in Java?

I know in Java, you just do this:

intArray[0][intCount] = strSIN.charAt(intCount) - '0';

I'm not sure what type did you declare strSIN as. As far as I know, there's no base string type available in C++ (well, I've not touch C++ for a few years now, maybe something new popup). String normally are represented as char[] or *char or CString. There are other representation, but are less popular. For char[] and *char, it should be easy as it's an array, "CharAt" would simply means char[at] (I forgotten how to do it with *char, but you can lookup in google).

As for CString, you need to refer to your class reference, since it could be from ATL, MFC or other lib, they all have different implementation.

EDIT: PS: I'm refering to windows here. If you're talking about linux, then CString might not applies


You may try this to see if it it help


for char*

int index = 0;char *message = "Hello, Java";char achar = *(message + index);orchar achar2 = message[index];

If I'am not forgot it, there is a class called string on STL.

But I'am not good with this class.


well, let me clarify that I use the string to store the SIN (Social Insurance Number) and I want to pick out the certain number out to analyze it.Many thanks,xboxrulz


Sorry, I haven't see this


Hi, for c++ string reference.

Here have a great web site.





It should not hard because it looks like java and c# string



For quick way

because the message pointer variable was 0-based index array

So, please change the index variable to point to the char you need.

then loop throught it, as,


#include "stdio.h"#include "string.h"int main(){ // change the index to start the looping int index = 2; // change this to the number of char required (+ 1 if including null char) int numrequiredchars = 8; // build a simple buffer to hold a new data char *requiredchars = new char[numrequiredchars]; // the source text char *message = "Hello, Java and long long text"; memset(requiredchars, 0, numrequiredchars); for (int i = index; i < numrequiredchars; i++) { *requiredchars = *(message + i); ++requiredchars; } printf("%s\n", requiredchars); //printf("\n%s\n", message);}

For relatively full story



I'am not good in explaination but I try my best,


char *message = "Hello, Java" means a char pointer variable that is pointing to a memory location in system that start with char H and contain a string Hello, Java

Because char *message = "Hello, Java" basically equal char[] = "Hello, Java".

So both of it are an char array, except that the formal including a null char at the end of char array ( the string )

And since a char is occupy 1 byte memory, so a pointer address = 1 byte, i.e.


0001 => H0002 => e

compared with an integer type that is 4 byte in size, i.e.


0001 => 10005 => 2

That result in,

*message = H*(message + 1) = e*(message + 2) = l...

If you just print out the message variable with printf,

it will show the address that it contains.

To get the actually value it pointed to,

use the * operator to dereference, as,




Since not very good in English.

So it might be something missed.


Wish this help !


Alright, I found an alternative.I'll keep this for reference.Many thanks,Adrien


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.