dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: use pointer

  1. #1
    Join Date
    Mar 2018
    Posts
    11

    use pointer

    I like to know if some one can show me a way to use pointer in main to argument in function ft?. If it is not possible, I like to know.
    Code:
    void f(int &v)
    {
    	v = 10;
    }
    main()
    {
      //is it possible to pass pointer to function f.
    
    }

  2. #2
    Join Date
    Nov 2018
    Posts
    12

    Re: use pointer

    Maybe:
    Code:
    #include<iostream>
    using namespace std;
    void f(int &v)
    {
        v = 10;
    }
    int main()
    {
        int a = 0;
        int *pa = &a;
        f(*pa);
        cout << a << endl;
    }

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,697

    Re: use pointer

    to use pointer in main to argument in function ft
    Sorry, but I'm not sure I understand the question. You can pass a pointer as an argument to f by specifying the parameter as type pointer such as:

    Code:
    void f(int* pv)
    {
        *pv = 10;
    }
    
    int main()
    {
        int a = 0;
        f(&a);
    }
    After the call to f, a will have the value 10. This is the 'c' way of doing pass-by-reference,

    Is this what you mean...?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.1.3)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,380

    Re: use pointer

    Quote Originally Posted by Y$EA View Post
    Code:
    void f(int &v)
    {
    	v = 10;
    }
    main()
    {
      //is it possible to pass pointer to function f.
    
    }
    The simple answer: no, just because the "function f" requires the argument of the type reference to integer.

    More complicated answer: it would be possible if one implemented an overloaded function having the argument of type pointer to integer like:
    Code:
    void f(int *v)
    {
    	*v = 10;
    }
    And now is my question:
    What are you trying to achieve and what for?
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2017
    Posts
    434

    Re: use pointer

    Quote Originally Posted by Y$EA View Post
    I like to know.
    Here's a video that explains (some of) the different ways to pass parameters to a function.

    https://www.youtube.com/watch?v=TgsH02sORZ0

    Note that just because something is possible doesn't mean one should do it. There exists a set of "best usage of C++" recommendations which represents the consensus view on what is best in different situations,

    https://github.com/isocpp/CppCoreGui...eGuidelines.md
    Last edited by wolle; May 7th, 2019 at 12:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)