-1

I want to have an Int pointer point to a double but am recieving the following error: "cannot convert 'double*' to 'int*' in assignment"

Say we have:

double myVar1; 
int *myPointer1 = new int; 

myPointer = &myVar1; (cannot convert 'double*' to 'int*' in assignment error)

How do I fix my code? I can't make the pointer a "double" because this is for a school assignment and I was specifically asked to use these data types.

Andrew
  • 19
  • 2
  • 4
  • 4
    Are you sure that's what you're supposed to do with your assignment? – πάντα ῥεῖ Feb 13 '17 at 00:54
  • 1) You can't do that. 2) Your code contains a memory leak (`new` without corresponding `delete`, and the pointer returned by `new` is lost). – melpomene Feb 13 '17 at 01:10
  • I am not sure, this wouldn't be the first time there was a typo in a homework assignment. To complete the assignment I just made the pointer a double. In my code I did have the corresponding delete. – Andrew Feb 13 '17 at 02:26

1 Answers1

2

You can write:

int *myPointer1 = reinterpret_cast<int *>(&myVar1);

However it will be undefined behaviour to use this pointer to try and access the double. So there is not really any reason to write this code.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365