pointer

This is a discussion on pointer within the C++ Programming forums, part of the General Programming Boards category; Code: char* p = new char[5]; Am I correct in saying that the above statment assigns 5 elements of type ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    pointer

    Code:
    char* p = new char[5];
    Am I correct in saying that the above statment assigns 5 elements of type char on the heap and that all elements contain '\0' ?

    I then add this line of code,
    Code:
    p = "this is my car";
    The above contains obviously more than 5 elements, does the compiler redefine the pointer an assign a big array ? does the compiler add '\0' at the end of the array ?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char* p = new char[5];
    This gives you a pointer to an array of 5 chars, I don't believe they will be initialised to any value.

    >>p = "this is my car";
    This assigns an address of a string literal to p. It does NOT copy the string literal into the newly allocated memory provided by the first line of code. In fact, using these two lines together is a bad thing, because you are throwing away a pointer to dynamically allocated memory, and therefore creating a memory leak.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Hammer
    [B]>>char* p = new char[5];
    This gives you a pointer to an array of 5 chars, I don't believe they will be initialised to any value.
    This is just of the top of my head
    Code:
    char* p = new char[15];
    
    for (int index=0; index < 15; index++)
       p[index] = a;
    
    cout << p << endl;
    The above would print out aaaaaa...... (15 times)

    How does the program know how many elements p contains ? There must be some kind of termination char.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The above would print out aaaaaa...... (15 times)
    Well, you need to do a couple of things to it first:

    >>p[index] = 'a';
    ...note the single quotes.

    And yes it needs a termination character \0 ( a char with value 0x00).

    >>char[14] = '\0';
    Note I used 14, not 15 to access the last element of the array.
    Also, it can only hold 14 a's, and 1 terminator.

    Why not try compiling some of your code and see for yourself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    see now...pointers tend to confuse me with stuff like that!
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Hammer
    >>The above would print out aaaaaa...... (15 times)
    Well, you need to do a couple of things to it first:

    >>p[index] = 'a';
    ...note the single quotes.

    And yes it needs a termination character \0 ( a char with value 0x00).

    >>char[14] = '\0';
    Note I used 14, not 15 to access the last element of the array.
    Also, it can only hold 14 a's, and 1 terminator.

    Why not try compiling some of your code and see for yourself.
    Thanks for your reply.

    I have compiled the program now, and it prints out 14 a without me setting a termination char. So I guess the compiler set the \0 in the last element ?

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    To handle char* is the hardest thing for beginners to learn.

    Someone ought to write a book that uses std::string and intruduces char* later when the student is more familiar with the language.
    I think there is a book like that, it might have been the "black book".
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    "Teach youself C++" teaches pointers in chapter part II i think. its like a quarter of the way through the book.

    [edit]
    forgot to mention the book is bigger than websters disctionary.
    [/edit]
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    not all char arrays are strings. Only char arrays that are null terminated are strings. Therefore:
    Code:
    char cArray[15];
    for(i = 0; i < 15; ++i)
    {
      cArray[i] = 'a';
    }
    is valid but cArray is not a string meaning you won't be able to do this:

    cout << cArray << endl;

    In order to display elements of cArray (as utilized above) to the screen you will need to use a loop like this:
    Code:
    for(i = 0; i < 15; ++i)
    {
      cout << cArray[i];
    }
    Doing so you will indeed have a row of 15 a's on the screen.

    To have cArray hold 15 a's as a string you will need to have cArray contain 16 char, 15 for the a's and 1 for the null terminating char.

    Code:
    char cArray[16];
    for(i = 0; i < 15; ++i)
    {
      cArray[i] = 'a';
    }
    cArray[++i] = '\0';
    cout << cArray << endl;
    
    //or
    
    char cArray[16];
    cout << "enter 15 a's in a row, then press enter" << endl;
    cin >> cArray;
    cout << cArray;
    the >> operator tacks on a null terminating char to the array for you, as will other istream methods like get() or getline().


    It is this ease of use, that is, no need to use loops to input and display elements of a char array, that makes strings so useful.


    Also note that when using the new operator:

    char * p;

    p = new char; //p is a pointer to space for a single char
    delete p;

    p = new char[16];//p (for all practical purposes) is now an empty char array, not a string, and won't be a string unless the char array is null terminated.
    delete [] p;

  10. #10
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    i still dont understand the whole purpose of pointser. can someone tell me a couple advantages to using them?
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21