char and char* (pointer)

83,958

Solution 1

Think of char* p; as of address in memory. You did not initialize this pointer so it does not point to anything, you cannot use it.

To be safe always:
either initialize pointer to zero:

char *p = 0; // nullptr in C++11

or initialize to some automatic

void foo() {
  char a[100];
  char *p = a;
}

or global memory:

char a[100];
void foo() {
  char *p = a;
}

or get dynamic memory:

char* p = new char [100];

Then you can use p (if not NULL) to read data and to read from p...


For your misunderstaning of operator >> (std::istream&, char* p). This operator expects that p points to some memory (automatic,global,dynamic - no matter) - it does not allocate memory by itself. It just reads characters from input stream until whitespace and copy it to the memory pointed by p - but p must already points to some memory.


For taking address of char q;. Of course you can take address of q: &q, and it type is char* p. But &q is different that p, and this q=*p just copies first character pointed by p to q, it cannot change address of q - its address is unchangeable. For cout << &q - operator << (ostream&, char* p) expects that p points to NULL terminated string - and &q points to memory containing "H" but what is after this character no one knows - so you will get some garbage on screen. Use cout << q to print single character.

Solution 2

First thing to learn and always remember about pointers is to make sure to allocate memory for them, otherwise your program won't run properly.

Your code should actually be modified as follows to allocate memory so that "cin" can write the user input into the allocated memory:

int main() {
    char *p = new char[1024];      // allocate memory so cin
    cin >> p;                      //forexample: haha
    char q = *p;
    cout << "&q = " << &q << endl; 
    cout << "q = " << q << endl;   
    return 0;
}

Now, a character pointer is a variable pointing to a position in memory holding a set of characters (not necessarily one character, maybe more than one, maybe none as in the case of the special value null), while a character variable is actually holding a single character (not a set of characters).

The basic operators when dealing with pointers is the &(address of) and *(value at). The & retrieves the address of a variable, so if we have [char q;] then [&q] would be a character pointer. On the other hand, the * retrieves the value at the given pointer, so if we have [char *p;] then [*p] would be the character in memory at which p is pointing.

Now back to your example, comments inline for illustration

int main() {
    // allocate a place of 1024 character in memory
    // and let p points to that place
    char *p = new char[1024];      

    // call cin to read input from the user and save
    // it in memory at the location pointed to by p
    // NOTE: cin would put an extra NULL character 
    // at the end to terminate the string
    cin >> p;                      //forexample: haha

    // Now p would be pointing to a piece of memory like this
    // [h] [a] [h] [a] [\0]

    // use the value at operator to de-reference the pointer
    // p, the result would be a single character which 
    // will be the first character in memory p is pointing at
    char q = *p;

    // printing the value of (the address of q)
    // Note you have a problem here as cout will
    // be handling the memory starting at the address 
    // of q as a string while it is not, so you
    // will get a string starting with "h" and followed
    // by whatever is there in memory by the time of execution
    cout << "&q = " << &q << endl;

    // printing the value of the character q
    cout << "q = " << q << endl; 
    return 0;
}

I hope it helps

Solution 3

You should have something like:

#include <iostream>
using namespace std;

at the top of your program. Or could omit the using and refer to std::cin and std::cout.

int main() {
    char *p;

p is a pointer, but you haven't initialized it, so it could point anywhere or nowhere. You need to initialize it, for example with:

p = new char[100];

...

    cin >> p;                      //forexample: haha

This is ok if you've initialized p to point somewhere -- except that you can overflow the buffer it points to if you enter too much data. That's ok for a simple test program like this, but in practice you'll want to take measures to avoid it.

    char q = *p;
    cout << "&q = " << &q << endl; //&q = h

&q is of type char*, a pointer to char. Sending a char* value to cout doesn't print the value of the pointer (a memory address); it prints the string that it points to. (Though I'm getting some odd results when I run it myself; I may be missing something.) If you want to see the pointer value, cast it to void*:

count << "&q = " << (void*)&q << endl;

(Or you could use one of the C++-specific casts; I'm not sure which is best.)

    cout << "q = " << q << endl;   //q = h

Here q is just a char, so this prints its value as a character: h.

    return 0;
}
Share:
83,958
Iburidu
Author by

Iburidu

Updated on July 13, 2022

Comments

  • Iburidu
    Iburidu almost 2 years

    I would like to understand how pointers work, so i created this small program. first of all i create a p pointer, which points to a char.

    The first question is at this point. If i create a pointer, the value of it is a memoryaddress (if i point it to a non-pointer object), but this time it is "haha" in my example. Why does it work this way in char*? And how i can add value to it with cin >> p?

    My second question is that, i created a q char, which has the value of the *p pointer at the point i created it. BUT its value and address are "h" too, but why? It must be the memory address of this char object! It's pointless :D (mingw - gcc)

    #include <iostream>
    
    int main() 
    {
     /* char *p;
        cin >> p;                      //forexample: haha */
    
        char * p = "haha";
        char q = *p;
        std::cout << "&q = " << &q << std::endl;   //&q = h
        std::cout << "q  = " <<  q << std::endl;   //q = h
    
        return 0;
    }
    

    MORE: If i allocate memory first with char a[100]; char *p=a; then &q = h»ŢĹ, so "h" and some mess. but it should be a memoryaddress! and my question is, why is not it address then?