-
July 28th, 2010, 07:28 PM
#1
Have got error pointer
Code:
void function(int & i)
{
i=5;
}
int main()
{
int a=2;
int *b=&a;
function(b);
printf(b);
return 1;
}
I cant print b
-
July 28th, 2010, 07:32 PM
#2
Re: Have got error pointer
Originally Posted by Dintey
I cant print b
You will have to fix the compiler errors first.
Since you are writing C++, include <iostream> and use std::cout to print to the screen, instead of using printf.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 28th, 2010, 07:53 PM
#3
Re: Have got error pointer
The function is expecting a reference to an int, but you are passing it an int* instead.
-
July 29th, 2010, 02:04 AM
#4
Re: Have got error pointer
Thank you
I fix it this way
Code:
#include <iostream>
using namespace std;
void function(int & i)
{
i=5;
}
int main()
{
int a=2;
int *b=&a;
function(&b);
cout<<b;
return 1;
}
An error has reported,
-
July 29th, 2010, 03:35 AM
#5
Re: Have got error pointer
#include <iostream>
using namespace std;
void function(int* i)
{
*i=5;
}
int main()
{
int a=2;
int *b=&a;
function(b);
cout<<*b;
return 1;
}
-
July 29th, 2010, 04:17 AM
#6
Re: Have got error pointer
Originally Posted by Dintey
An error has reported,
Look at these lines
Code:
int a=2;
int *b=&a;
function(&b);
In the second line you take a pointer to the object a by preceding the variable with an ampersand. In the third line you take a pointer to the object b. Since b has type int*, &b has type int** (pointer to pointer to int). If instead you want to take get the value of what b points to, use *b.
Also, here
you are printing the value of b, i.e. the memory address it is pointing to. You can get the value of what b is pointing to in the same way as above.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 29th, 2010, 04:39 AM
#7
Re: Have got error pointer
Thanks
@chandrasekaran1987..... but I don't want to change it into pointer (in the function)
@D_Drmmr..... Can you write it for me please ?
-
July 29th, 2010, 06:40 AM
#8
Re: Have got error pointer
Originally Posted by Dintey
Thanks
@chandrasekaran1987..... but I don't want to change it into pointer (in the function)
@D_Drmmr..... Can you write it for me please ?
Why use a pointer at all? What's wrong with
Code:
#include <iostream>
using namespace std;
void function(int& i)
{
i=5;
}
int main()
{
int a=2;
function(a);
cout<< a;
return 1;
}
If you really want the pointer in main
Code:
#include <iostream>
using namespace std;
void function(int& i)
{
i=5;
}
int main()
{
int a=2;
int* b = &a;
function(*b);
cout<< *b;
return 1;
}
-
July 29th, 2010, 10:03 AM
#9
Re: Have got error pointer
Originally Posted by Dintey
@D_Drmmr..... Can you write it for me please ?
I could, but I generally don't do that since I don't believe it will help you. If you want to see an example then search for one online and you'll find plenty. Then again, I can't imagine that the book you are studying (or your teacher) didn't provide any examples that show how to dereference a pointer. Reapplying that knowledge in another situation is what learning is all about. If you just copy a piece of code written by someone else, you have learned nothing - and that will come to haunt you for sure.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|