Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
XML
#include<iostream>

using namespace std;

int main()
{
    double *iii=123;
    cout << *(iii);
    cin.get();
    return 0;
}

this is a simple code using pointers, why dosen't it work
why dose the compiler thwor an aeeor saying "invalid conversion from int to int*"
Posted
Comments
joshrduncan2012 2-Oct-13 9:05am    
Have you tried debugging the code to see where the problem is? What makes you think we will answer a question without any effort on your part first to see what the problem is?
A.U.K 5-Oct-13 8:45am    
well, i did!
and the compiler says, " invalid conversion from int to int* ".
i din't understand that, i tried making so many adjustmentes, but it simply repeats the same error.

Well... lets look at it this way.
double *iii
declares a variable called "iii" which is a pointer to a double. Which means that it contains the address of double values, hence the name "pointer" - it "Points" to the value.
123
Is a number which can be treated as a double.
So
C++
double *iii=123
Declare a pointer to a double and tries to put a double value into it. Not the address of a double, but the value itself.

If you think of a car key as the "pointer" to a car then you are trying to drive the key instead of the car!

I think you need to go back to your lecture notes and read through this stuff again, but the first line could be made to work if you did this:
C++
double vd = 123;
double *iii=&vd;
Because in this case you are putting the address of a double into the pointer. Note that you can't take the address of a constant:
C++
double *iii=&123;
because then you could try to modify constants and the PC Pixies don't like you trying to do that...
 
Share this answer
 
Comments
[no name] 2-Oct-13 9:20am    
You can however declare the pointer as a pointer to a constant and then set it to the address of a constant. It must be the address of an l value and so &123 won't do.
OriginalGriff 2-Oct-13 9:44am    
Yep - but I figured the OP had enough problems at the moment! :laugh:
Sergey Alexandrovich Kryukov 2-Oct-13 12:21pm    
Explained, a 5. :-)
—SA
Quote:
double *iii=123;

The above line is wrong (a pointer must point. You need something like:
C
double d = 123.0;
double *iii; // pointer variable declaration
iii = &d; // pointer variable initialisation

You may short-cut the last two lines into:
C
double *iii = &d; // pointer variable declaration and initialisation
 
Share this answer
 
v2
You need to separate the declaration and initialization, like;
C++
double *iii;
*iii = 123;

In your original code you are trying to set the pointer iii to the value 123. And since iii is a pointer it needs to be set to the address of a value, rather than the value itself. And you cannot do this with a direct constant so you need to split the expression as above.
 
Share this answer
 
Comments
CPallini 2-Oct-13 9:32am    
Are you really suggesting the OP to de-reference a uninitialised pointer? :-)
Richard MacCutchan 2-Oct-13 9:38am    
Damn, what's happened to my brain?
[no name] 2-Oct-13 9:47am    
Not just yours, others fell for the (deliberate?) mistake.
Richard MacCutchan 2-Oct-13 11:54am    
Indeed, someone even gave me a 5.
C#
#include<iostream>

using namespace std;

int main()
{
    double *iii=NULL;
    *iii = 11.13;
    cout<<*iii;
    cin.get();

    return 0;
}


pointer must be initialized with null at the time of declaration.
declared double pointer and initialized to const int.

after declaration now assign the double value into double pointer.
 
Share this answer
 
C++
double *iii;


iii is a pointer of double. you can't assign value to it. you can assign only double variable address to it.
you can do it as follows,

C++
#include<iostream>
 
using namespace std;
 
int main()
{
    double val = 123.0;
    double * iii = &val;
    cout<<*iii;
    cin.get();
 
    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900