Using and Initializing Pointers

I'm having trouble understanding how pointers work. I tried playing around with them and found some curious results. And based on the theory provided by my book and tutorials, the results are off.

The following compiles but crashes (stops working)
1
2
    char* ch;  //create a pointer to one byte of memory?
    cout << ch;

Output: None (crashes)
Shouldn't the compiler give ch a memory address? Why can't it print it? No difference if I use & or *

Next, this compiles and runs:
1
2
3
4
    char* ch;
    ch = "5";
    cout << ch;
    return 0;

Output: 5
But why aren't I getting an address? Shouldn't I have to say: cout << *ch to get the value being pointed at by ch? And to assign a value to ch, shouldn't I have to do this: *ch = "5" (in order to dereference ch as a pointer and say I'm assigning 5 to the value ch is pointing at).
In the piece of code above, what we are doing is assigning string 5 to a pointer (as per theory). Why is this working? And if this works, why doesn't the following work (doesn't compile):
1
2
3
    char* ch;
    ch = '5';
    return 0;


Even more curiously:
1
2
3
    int* x;
    cout << x;
    return 0;

Behaves as expected.
Output: a valid address.
So whats different with char pointer?

AND THEN (I know right?) This compiles but crashes:
1
2
3
    int* x;
    *x = 5;
    return 0;

Why can't I assign a value to the memory allocated for x?
Last edited on
closed account (3CXz8vqX)
char * ch;
cout << ch;

Works for me. It prints a random string of characters. As it should because a string is not really much different.

Try printing *ch too =D

If you want the address of ch, you'll need the & reference. &ch;

Edit

5...isn't a valid memory address?
Last edited on
anonymousxyz wrote:
Shouldn't the compiler give ch a memory address? Why can't it print it? No difference if I use & or *


I don't think this will work because you never assigned a memory address to ch.
1
2
    char* ch;  //create a pointer to one byte of memory? 
cout << ch;


This should work
1
2
 char* ch;//create a pointer to one byte of memory?= 
cout << &ch;
Last edited on
std::cout treats pointers to char in a special way, so that, for instance, std::cout << "a string literal" ; will print out a string literal instead of the address that's fed to cout.

If you want to display the address that a pointer to char holds, then cast it to another type:

1
2
3
4
5
6
7
#include <iostream>

int main()
{
    char* ch ;
    std::cout << static_cast<void*>(ch) << '\n' ;
}


1
2
    char* ch;  //create a pointer to one byte of memory?
    cout << ch;

causes undefined behavior. ch is treated as a pointer to a c style string and dereferenced. Not good.


Next, this compiles and runs:

1
2
3
4
    char* ch;
    ch = "5";
    cout << ch;
    return 0;


Output: 5
But why aren't I getting an address? Shouldn't I have to say: cout << *ch to get the value being pointed at by ch?


*ch is a single char. "5" is an array of characters consisting of '5' and '\0'. If you were to cout << *ch the output would also be 5.


And to assign a value to ch, shouldn't I have to do this: *ch = "5" (in order to dereference ch as a pointer and say I'm assigning 5 to the value ch is pointing at).

*ch is a single char. "5" is an array of char. You can't assign one to the other. On the other hand you can make a pointer to char point at an array of char.


Even more curiously:

1
2
3
    int* x;
    cout << x;
    return 0;


Behaves as expected.
Output: a valid address.
So whats different with char pointer?
cout interprets a pointer to char as a C-style string. Obviously, there's no reason to treat a pointer to int in the same way.

AND THEN (I know right?) This compiles but crashes:
1
2
3
    int* x;
    *x = 5;
    return 0;


Why can't I assign a value to the memory allocated for x?


You can. By saying x = some_pointer_value. x is a pointer that hasn't been pointed at any memory and has some random value, so when you dereference it (*x) you're interpreting that random value as an address and assigning the memory that it points to a value. In other words, you're trashing memory you don't own -- undefined behavior.

Ahh, I appreciate the help a lot!
Topic archived. No new replies allowed.