-
Question about pointers
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?
-
Your first version will not work. What you want to do is use the function strcpy or strncpy. The second function takes as an aditional parameter, the number of characters to copy.
Here is an example of how you would use it.
Code:
char *pString = new char[6];
// Did memory get allocated correctly ?
if( pString )
{
strcpy( pString, "Hello" );
}
delete [] pString;
Remember in your example string is a pointer. It points to a memory address. When you tried to assign a string literal to it that will cause a problem. Your second example would work but not give you the results you expect. You are assigning the integer values 0 through 9. These represent different ascii characters than '0' '1', etc. You would have to do something like
Code:
for( int i = 0; i < 10; ++i )
{
string[i] = '0' + i;
}
That would give you the correct ascii value. Also you are correct. You want to add one element to your allocation for the null terminator. If you assign each element individually you will have to manually put the terminator in. If you use strcpy it will do that for you.
-
Thanks for the help Mr. Wizard, I totally forgot about strcpy. It's been a while since I did any real programming.
-
1) Not valid
2) Not valid
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.
Instead:
Code:
char* string; //create a string and initialize memory for 10 characters
string = "123456789"; //use the equals operator to set it equal to a 9 character string.
Note: This only works for string constants. Otherwise you must use the strcpy() function in <cstring>.
>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;
>}
Instead:
Code:
char* string = new char[10]; //same as above
int x;
for (x = 0; x < 10; x++) //loop through the string setting each cell in the array to 1-9 same as above
{
string[x] = '0' + x;
}
string[x] = '\0';
-
Your 2nd example needs new char[11] to safely store the \0 at the end
-
It sure does. I completely missed that. :eek: