CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    13

    Unhappy 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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Have got error pointer

    Quote Originally Posted by Dintey View Post
    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Have got error pointer

    The function is expecting a reference to an int, but you are passing it an int* instead.

  4. #4
    Join Date
    Jun 2010
    Posts
    13

    Unhappy 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,

  5. #5
    Join Date
    Mar 2009
    Location
    chennai
    Posts
    7

    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;
    }

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Have got error pointer

    Quote Originally Posted by Dintey View Post
    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
    Code:
       cout<<b;
    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

  7. #7
    Join Date
    Jun 2010
    Posts
    13

    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 ?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Have got error pointer

    Quote Originally Posted by Dintey View Post
    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;
    }

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Have got error pointer

    Quote Originally Posted by Dintey View Post
    @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
  •  





Click Here to Expand Forum to Full Width

Featured