Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:19:00+00:00 2026-05-27T08:19:00+00:00

void func(char* buf) { buf++;} Should I call it passing by pointer or just

  • 0
void func(char* buf) { buf++;}

Should I call it passing by pointer or just passing by value(with the value being pointer type)? Would the original pointer passed in be altered in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:19:00+00:00Added an answer on May 27, 2026 at 8:19 am

    This is passing by value.

    void func( char * b )
    {
        b = new char[4];
    }
    
    int main()
    {
        char* buf = 0;
        func( buf );
        delete buf;
        return 0;
    }
    

    buf will still be 0 after the call to func and the newed memory will leak.

    When you pass a pointer by value you can alter what the pointer points to not the pointer itself.

    The right way to do the above stuff would be

    ALTERNATIVE 1

    void func( char *& b )
    {
        b = new char[4];
    }
    
    int main()
    {
        char* buf = 0;
        func( buf );
        delete buf;
        return 0;
    }
    

    Notice the pointer is passed by reference and not value.

    ALTERNATIVE 2

    Another alternative is to pass a pointer to a pointer like

    void func( char ** b )
    {
        *b = new char[4];
    }
    
    int main()
    {
        char* buf = 0;
        func( &buf );
        delete buf;
        return 0;
    }
    

    Please note I am not in any way advocating the use of naked pointers and manual memory management like above but merely illustrating passing pointer. The C++ way would be to use a std::string or std::vector<char> instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example C API signature: void Func(unsigned char* bytes); In C, when I want to
I am writing a functor F which takes function of type void (*func)(T) and
I wanted to call Test1() Method Within WaitAndCallFunc() Function. Code: typedef void (*func)(); void
Say a function takes a void pointer as an argument, like so: int func(void
I have a the following code: #include <iostream> using namespace std; void func(char *
I have a function void func(int x, char *str, ...) { ... } I
How can I template these functions? boost::function< void(int) > binder( void(*func)(int, char), int a1,
In C++ I would do something like this: void some_func(const char *str, ...); some_func(hi
I tried two ways: void func(const char *path, const char *arg0, ...){ va_list args;
I have a C++ API prototype void Func(int& size); How can I translate it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.