I am a budding C/C++ programmer and so I am of course having trouble dealing with the concept of pointers. Not pointers in and of themselves, but using pointers as strings. I have several questions but will post them seperately. My first question is about setting a pointer equal to a string. I will present 2 snippets of code, please tell me which, if any, is valid and why. Thank you for the help everyone.
1)
char* string = new char[10]; //create a string and initialize memory for 10 characters
string = "123456789"; //use the equals operator to set it equal to a 9 character string.
2)
char* string = new char[10]; //same as above
for (int x = 0; x < 10; x++) //loop through the string setting each cell in the array to 1-9 same as above
{
string[x] = x;
}
So, basically what I want to know is, can you set a pointer equal to a string, or do you have to set each cell manually? My other question is, if I have allocated enough space for 10 characters, and put 10 characters into it, is that overflowing memory because of the null terminator at the end?