Normal variable and pointer
Can you explain about bellow code?Why (na+1)=0x4? when a variable store stack or heap ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// Example program
#include <iostream>
#include <string>
int main()
{
int *name= new int;
std::cout << "Address, " << &name << "\n";
std::cout << " Value, " << name << "\n\n";
int nam;
std::cout << "Address, " << &nam << "\n";
std::cout << " Value, " << (nam+1) << "\n\n";
int *na;
std::cout << "Address, " << &na << "\n";
std::cout << " Value, " << (na+1) << "\n";
}
|
1 2 3 4 5 6 7 8 9
|
Address, 0x72b975e06c60
Value, 0xda5750
Address, 0x72b975e06c5c
Value, 1
Address, 0x72b975e06c68
Value, 0x4
|
Last edited on
It's just an arbitrary value since you never initialized it. Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
int main()
{
int *a = new int;
*a = 123;
std::cout << "Address: " << &a << "\n";
std::cout << " Value: " << a << "\n";
std::cout << " *Value: " << *a << "\n\n";
int b = 234;
std::cout << "Address: " << &b << "\n";
std::cout << " Value: " << b << "\n\n";
int *c = &b;
std::cout << "Address: " << &c << "\n";
std::cout << " Value: " << c << "\n";
std::cout << " *Value: " << *c << "\n";
}
|
Address: 0x78b000607b60
Value: 0x27f6e40
*Value: 123
Address: 0x78b000607b5c
Value: 234
Address: 0x78b000607b68
Value: 0x78b000607b5c
*Value: 234
|
Last edited on
Topic archived. No new replies allowed.