Hi
I have a small code and a question concern it.
What is the value of the first print of ptr2. I have 0. Why? Isn't suppose to be a memory address like for example 0xff10
thanks

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
  //a: 0xdf30
  //b: 0xef41
  //ptr1: 0xff00
  //ptr2: 0xff10
  
  int a, b;
  int *ptr1, *ptr2;
  
  a = 3200;
  ptr1 = &a;
  b = *ptr1;
  cout << a <<" " <<b << " "<<ptr1 <<" "<< ptr2 << endl;
  ptr2 = ptr1;
  cout << ptr2<<" " ;
  cout << *ptr2<<" " ;
  cout << *&ptr2 <<endl;
  (*ptr1)++;
  cout << ptr1 <<" "<< *ptr1 << " "<<a << endl;
  getch();
  return 0;
}

which line ? On line 18 the value of ptr2 is random because it is uninitialized. Line 22 is also random because it is deferencing the integer located at the address of the address, which also has not been initialized and could possibly crash the program.

I don't know how to get cout to print the address of a pointer -- printf() uses "%p" but don't know about cout.

Hi
Actually I think you are wrong about the 20th line. In 19th line you have assignment, that makes an ptr2 equall to adress of ptr1. I am wandering about the addess ptr2 in the line 18th. I thought (just like you) that it will be random but when I compile it sais 0. So looks like its not an adrress, what could be than?
thanks

line 18 is executed before line 19 so when line 18 is executed ptr2 is uninitialized. You must initialize pointers and other objects before (not after) attempting to use them.

>> I thought (just like you) that it will be random but when I compile it sais 0

When I execute your program I get this output, after getting an error message

3200 3200 0012FF40 CCCCCCCC
0012FF40 3200 0012FF40
0012FF40 3201 3201

The value of 0 may be random -- whatever is at the memory location at the time. Apparently, 0 is at that memory location.

You are probebly right. Let say I belive you. That would explain all of my questions.
Thank you appreciate it

>>Let say I belive you.
Ok, Ok you caught me. I lied :twisted:

>>I don't know how to get cout to print the address of a pointer
I see from my previous post that its actually easier to print the address using cout than it is with printf(). :)

which line ? On line 18 the value of ptr2 is random because it is uninitialized. Line 22 is also random because it is deferencing the integer located at the address of the address, which also has not been initialized and could possibly crash the program.

I don't know how to get cout to print the address of a pointer -- printf() uses "%p" but don't know about cout.

Something like this will probably work, provided unsigned long is large enough to hold a pointer

#include <iostream>

int main()
{
    char* hello("Hello, World");
    unsigned long addr = reinterpret_cast<unsigned long>(hello);
    std::cout << addr << '\n';
}

(MSVC++ throws a warning about pointer truncation, although the warning goes away if I switch the cast to the nonstandard __int64 type)

>>(MSVC++ throws a warning about pointer truncation
VC++ 2005 has a switch in properties to turn off 64-bit warnings

Properties --> Configuration Properties --> C/C++ --> General, "Detect 64-Bit Portability Issues". Set it to NO and you won't get that warning. In 64-bit compilers sizeof(any pointer) != sizeof(long)

This article has been dead for over six months. Start a new discussion instead.