one char from string to char array position..

I have a char array in which I save n chars.
char * my_char;
my_char = new char [10];

OK, Now I have a string . I want to save the 4th character in my_char
string a_string = "nothing";
my_char[4] = string.substr(4,1).c_str();

I have a compile error :
invalid conversion from 'const char*' to 'char'
How can I do this ?

I am starting to hate the char array ....

Thanks.
Last edited on
string has the operator[] overloaded so you can simply write my_char[4] = a_string[4]

Make sure that 'a_string' really contains 4 chars
Examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdlib.h>
#include <string>
using namespace std;

int main(){
	char * my_char;
	my_char = new char [10];
	string a_string = "nothing";

	//Example:
	strcpy(my_char,a_string.substr(0,1).c_str()); //save the 1st character in my_char
	//strcpy(my_char+0,a_string.substr(0,1).c_str());
	printf("my_char[0] = %c\n", my_char[0]);

	*(my_char+1) = *(a_string.c_str()+1);  //save the 2nd character in my_char
	printf("my_char[1] = %c\n", my_char[1]);

	*(my_char+2) = a_string[2];  //save the 3rd character in my_char
	printf("my_char[2] = %c\n", my_char[2]);

	my_char[3] = a_string[3];  //save the 4th character in my_char
	printf("my_char[3] = %c\n", my_char[3]);

	strcpy(my_char+4,a_string.c_str()+4); //save the 5th character in my_char
	printf("my_char[4] = %c\n", my_char[4]);

	strncpy(my_char+5,a_string.c_str()+5, 1); //save the 6th character in my_char
	printf("my_char[5] = %c\n", my_char[5]);

	memcpy(my_char+6,a_string.c_str()+6, 1); //save the 7th character in my_char
	printf("my_char[6] = %c\n", my_char[6]);

	printf("my_char = %s\n", my_char);
	
	system("PAUSE"); 
	return 0;
}
oh! thank you very much.
Topic archived. No new replies allowed.