Thread: Question about pointers

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    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?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Thanks for the help Mr. Wizard, I totally forgot about strcpy. It's been a while since I did any real programming.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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';
    Last edited by swoopy; 06-18-2004 at 07:51 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Your 2nd example needs new char[11] to safely store the \0 at the end
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It sure does. I completely missed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM