7

I want to save int value to a pointer variable. But I get an error:

#include <iostream>
using namespace std;

int main()
{
  int *NumRecPrinted = NULL;
  int no_of_records = 10;
  NumRecPrinted = (int*)no_of_records; // <<< Doesn't give value of NumRecPrinted

  cout << "NumRecPrinted!" << NumRecPrinted;
  return 0;
}

I tried doing this but I get 0 as return:

int main()
{
    int demo(int *NumRecPrinted);
    int num = 2;
    demo(&num);
    cout << "NumRecPrinted=" << num;    <<<< Prints 0
    return 0;
}

int demo (int *NumRecPrinted)

{
    int no_of_records = 11;
    NumRecPrinted = &no_of_records;
}

NumRecPrinted returns as 0

AJ.
  • 2,536
  • 9
  • 44
  • 80

6 Answers6

10

It's sometimes useful to "encode" a non-pointer value into a pointer, for instance when you need to pass data into a pthreads thread argument (void*).

In C++ you can do this by hackery; C-style casts are an example of this hackery, and in fact your program works as desired:

#include <iostream>
using namespace std;

int main()
{
  int *NumRecPrinted = NULL;
  int no_of_records = 10;
  NumRecPrinted = (int*)no_of_records;

  cout << "NumRecPrinted!" << NumRecPrinted; // Output: 0xa (same as 10)
  return 0;
}

You just need to realise that 0xa is a hexadecimal representation of the decimal 10.

However, this is a hack; you're not supposed to be able to convert ints to pointers because in general it makes no sense. In fact, even in the pthreads case it's far more logical to pass a pointer to some structure that encapsulates the data you want to pass over.

So, basically... "don't".

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
  • 1
    But if you must ... use [intptr_t](http://stackoverflow.com/questions/6326338/why-when-to-use-intptr-t-for-type-casting-in-c) – Useless Feb 09 '12 at 14:24
  • 1
    ... or at least `static_assert(sizeof(int) >= sizeof(void*), "the size of int must be greater than or equal to the size of a pointer");`, i.e., document your assumptions. – R. Martinho Fernandes Feb 09 '12 at 14:34
5

You want to be doing this:

NumRecPrinted = &no_of_records;

i.e. you're taking the address of no_of_records and assigning it to NumRecPrinted.

And then to print it:

cout << "NumRecPrinted!" << *NumRecPrinted;

i.e. you're dereferencing NumRecPrinted which will get the int stored at the memory address pointed to by NumRecPrinted.

mattjgalloway
  • 34,562
  • 12
  • 96
  • 109
3
#include <iostream>
using namespace std;

int main()
{
int *NumRecPrinted = NULL; // assign pointer NumRecPrinted to be valued as NULL
int *NumRecPrinted2 = NULL;
int no_of_records = 10; // initialize the value of the identificator no_of_records 
NumRecPrinted = (int*)no_of_records; // sets a pointer to the address no_of_records
NumRecPrinted2 = &no_of_records; // gives a pointer to the value of no_of_records

cout << "NumRecPrinted!" << NumRecPrinted;  // address of no_of_records 0000000A
cout << "NumRecPrinted!" << *NumRecPrinted2; // value of no_of_records 10
system("pause"); // ninja 
return 0;
}
Dzek Trek
  • 338
  • 3
  • 14
1

Here is the corrected version:

#include <iostream>
using namespace std;
int main()
{
  int *NumRecPrinted = NULL;
  int no_of_records = 10;
  NumRecPrinted = &no_of_records; // take the address of no_of_records

  cout << "NumRecPrinted!" << *NumRecPrinted; // dereference the pointer
  return 0;
}

Note the added ampersand and the asterisk.

NPE
  • 464,258
  • 100
  • 912
  • 987
1

(int *)no_of_records gives you a pointer to the address no_of_records. To get a pointer to the value of no_of_records, you need to write &no_of_records.

Abrixas2
  • 3,157
  • 1
  • 19
  • 22
0

I really like using union for this sort of stuff:

#include <iostream>
using namespace std;

int main()
{
  static_assert(sizeof(int) == sizeof(int*));

  union { int i; int* p; } u { 10 };

  cout << "NumRecPrinted! " << u.p;
  return 0;
}
Gigi
  • 4,876
  • 23
  • 24