A Negative integer Address for a pointer

Can someone tell me why this program sometimes outputs a negative integer address for my variable?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstring>

using namespace std;

void get_input(char *char_ptr, int MAX);

void reverse_string(char *char_ptr);

void display_result(char *char_ptr);

void print_address(char *char_ptr, const int &MAX);

bool repeat_program();

int main()
{
    const int MAX = 50;
    char user_entry[MAX];
    char* char_ptr;
    bool repeat;
    char_ptr = user_entry;
    do{
        get_input(char_ptr, MAX);

        reverse_string(char_ptr);

        display_result(char_ptr);

        print_address(char_ptr, MAX);

        repeat = repeat_program();
    }while (repeat == true);

    return 0;

}

void get_input(char *char_ptr, int MAX)
{
    cout << "Please enter a string" << endl;
    cin.getline(char_ptr, MAX + 1);
    cout <<"The string before reversing: " << *char_ptr << endl;
}

void reverse_string(char *char_ptr)
{
    char *front, *back, temp;
    front = char_ptr;
    back = char_ptr + strlen(char_ptr) - 1;
    while (front < back){
        temp = *front;
        *front = *back;
        *back = temp;
        front ++;
        back --;
    }
}

void display_result(char *char_ptr)
{

    cout << "The string after reversing: ";
    for (int i = 0; *(char_ptr + i) != '\0'; i++)
    {
         cout << *(char_ptr + i);
    }
    cout << endl;
}

void print_address(char *char_ptr, const int& MAX)
{
    cout <<"The address of variables \"char_ptr\" is: " << (long)char_ptr << endl;
    cout <<(long)&char_ptr<<endl;

}

bool repeat_program()
{
    char repeat;
    cout << "Would you like to go again? Y/y for yes, any other char for no" << endl;
    cin >> repeat;
    cin.ignore(1000, '\n');
    if (toupper(repeat) == 'Y')
        return true;
    else
        return false;
}


Sameple Dialogue:
Please enter a string
sdfdsf
The string before reversing: s
The string after reversing: fsdfds
The address of variables "char_ptr" is: -239338872
-239338928
Would you like to go again? Y/y for yes, any other char for no
Last edited on
Displaying addresses as signed decimal numbers isn't particularly helpful. You need to show it as hex.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int s = 3;
	cout << "s=" << s << " address of s=0x" << hex << &s << endl;
}
1
2
3
4
5
6
7
8
void print_address( char* char_ptr, const int& )
{
    // #include <cstdint>
    // uintptr_t (optional)    unsigned integer type capable of holding a pointer
    // http://en.cppreference.com/w/cpp/types/integer
    std::cout << "The address of variables \"char_ptr\" is: " << reinterpret_cast<std::uintptr_t>(char_ptr) << '\n' ;
    std::cout << reinterpret_cast<std::uintptr_t>(&char_ptr) << '\n' ;
}
std::hex, std::showbase etc have no effect on the output format of pointers.
The std::printf conversion specifier %p is used (an implementation defined character sequence for a pointer.)
http://coliru.stacked-crooked.com/a/0180d5feb8a45c52
http://rextester.com/LHKAI92267
Topic archived. No new replies allowed.