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