Thread: char* behaves strangely

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    32

    char* behaves strangely

    Hello,

    I've cast the address of a string to a char* . I expected to be able to use the pointer in the same way as other types, but it behaves quite differently. Could someone explain what is going on?

    Why does it print the string and not the address? How does it know where the string ends? Ho *do* I print the address of a char in the string, like for e[2] (I would expect this to be the address of the 3rd letter, s)?

    Here is the code. I've compared it to an int*.
    Code:
    #define DEBUG
    #ifdef DEBUG                                    
    #define P(A) cout <<  #A << ": " << (A) << endl;// prints variables
    #define NL cout << endl;                        // new line
    #else
    #define P(A)                                    // empty definitions
    #define NL
    #endif
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main() {
    
      int a[2];
      a[0] = 5; a[1] = 7;
      string s = "test text";
      
      int* ap = a;
      char* e = (char*)s.c_str();  // string elements
    
      NL; P(s); P(&s);
    
      NL; P(e); P(e[0]); P(&e); P(&e[0]); P(&e[2]); P(&(e[0]));
    
      NL; P(ap); P(ap[0]); P(&ap); P(&ap[0]); P(&ap[1]); P(&(ap[0]));
    }
    And here is the output

    Code:
    s: test text
    &s: 0012FF7C
    
    e: test text
    e[0]: t
    &e: 0012FF40
    &e[0]: test text
    &e[2]: st text
    &(e[0]): test text
    
    ap: 0012FF50
    ap[0]: 5
    &ap: 0012FF44
    &ap[0]: 0012FF50
    &ap[1]: 0012FF54
    &(ap[0]): 0012FF50

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    char*

    this is because at initialization of a char pointer the memory is allocated for the pointer. For example

    char* f = "c:\\my documents\\myfile.txt";


    is like saying

    char* f;

    f = new char[ n + 1 ]; \\where n matches the number of chars

    strcpy(f, "c:\\my documents\\myfile.txt");

    ....
    now if instead I did this this should not work...
    char* f;

    f = "c:\\my documents\\myfile.txt";

    needles to say...
    cout sees an address it prints what it believes to be a string

    if instead

    you remove the * from the int declaration you will get an address...
    zMan

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    char *text=0;

    text="This will work"


    MyClass *newclass;

    newclass=new MyClass();



    This doesn't work? Yes, it does.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The pointer returns c_str() is read only. I think alot of c++ string implementations use referance counting so this could cause a problem if you write something like

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
        string s = "Hello World!\n";
        string r = s;
    
        *(char*)r.c_str() = 'A';  // modify r should not modify s
    
        cout << "s = " << s;
        cout << "r = " << r;
    
        return 0;
    }
    It works (does not work) on g++
    s = Aello World!
    r = Aello World!

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thanks for the answers guys.

    Bubba wrote:
    char *text=0;

    text="This will work"
    How does the program know not to read any more characters from memory?


    zMan wrote:
    now if instead I did this this should not work...
    char* f;

    f = "c:\\my documents\\myfile.txt";
    I believe this works in exactly the same way as Bubba's code. Only the pointer is undetermined at start.


    Nick, I'm a beginner so I don't know much about string, but if the string variable is a pointer to a char array then that would be the correct result, because r and s are pointing to the same memory space. I got the same from the Borland compiler when I ran your code.

    I also would like to know the answers to these questions:

    How do I get the address that a char* is pointing to? I've tried to prefix with & but I believe this produces the address of the pointer itself.

    How would I get the address of any character that is in a character array?

    Seron

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >How does the program know not to read any more characters from memory?

    because the string literal "This will work" will be null terminated.

    >I believe this works in exactly the same way as Bubba's code. Only the pointer is undetermined at start.

    Yep, "c:\\my documents\\myfile.txt" is a string literal. You're not storing this string at the memory pointed to by f, but pointing f at the string literal.

    >How do I get the address that a char* is pointing to?

    You can get the address of it's first element using array notation -

    &f[0];

    >How would I get the address of any character that is in a character array?

    You can get the address the same way as above, but use the appropriate index.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Sorensen wrote:
    >How do I get the address that a char* is pointing to?

    You can get the address of it's first element using array notation -

    &f[0];

    >How would I get the address of any character that is in a character array?

    You can get the address the same way as above, but use the appropriate index.
    Ok, but how would I display those addresses? I've tried the below code and it doesn't work.

    Code:
    #include <iostream>
    using namespace std;
    
    void main() {
    
      char c[] = "test text";
      			// output
      cout << c << endl;	// test text
      cout << c[2] << endl;	// s
      cout << &c << endl;	// 0012FF80	// is this the address the pointer is pointing to
      					// or the address of the pointer variable ?
      cout << &c[0] << endl;// test text
      cout << &c[2] << endl;// st text
    }
    Seron

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could use printf, or -

    cout << hex << (long)&c[0];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy working strangely in msvc 2005
    By *DEAD* in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 09:50 AM
  2. ReadConsoleInput behaves bad.
    By antex in forum Windows Programming
    Replies: 2
    Last Post: 10-25-2005, 12:02 PM
  3. combobox reacts strangely to a mouse
    By johny145 in forum Windows Programming
    Replies: 2
    Last Post: 03-07-2005, 05:32 PM
  4. MSVC behaves strange
    By ripper079 in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2003, 08:34 PM
  5. being a GPL ***** feels strangely fufilling...
    By moi in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 08-06-2002, 02:45 PM