How to pass char* as refrence in a funstion?

Dani AI

Generated

Both answers in this thread are correct at the language level: described the C-style way (pass the pointer's address) and gave the C++ reference-to-pointer alternative. 's rule of thumb — pass an address when a callee must modify a caller's variable — is a good quick guide. The original discussion covered syntax; the important real-world concerns left out are ownership, lifetime, safety, and modern alternatives.

Ownership and lifetime are the primary issues. If the callee allocates memory, the contract must say who frees it, and matching allocation/deallocation is essential (new[] goes with delete[], not delete). Returning or storing a pointer to a local automatic buffer is undefined behavior. When filling a caller-supplied buffer, always pass the buffer capacity (size_t) to avoid overruns. For read-only input prefer const char* or, in modern code, std::string_view (C++17+).

Prefer RAII in C++ rather than raw new/delete. Using std::string for text or std::vector<char> / std::unique_ptr<char[]> for raw buffers gives clear ownership and exception safety. Example patterns:

void fillString(std::string &out) {
    out.resize(256);              // ensure space
    // write into &out[0] or out.data() (C++17+)
    memcpy(&out[0], "hello", 6);
}
std::unique_ptr<char[]> make_buffer(size_t n) {
    auto p = std::make_unique<char[]>(n);
    // fill p.get()...
    return p;                     // ownership transferred safely
}

Quick checklist: prefer returning an owning type (std::string/unique_ptr) when the function allocates; use pointer-to-pointer or reference-to-pointer only when interoperating with legacy C APIs or when changing a raw pointer must be explicit; document ownership and lifetime; always check/initialize pointers (use nullptr); avoid manual memory management when RAII will do.

Recommended Answers

All 10 Replies

pass char** which is a pointer to a pointer, like this

void foo( char ** ptr)
{
   *ptr = new char[255];
}

int mian()
{
   char *ptr = 0;
   foo( &ptr );

   <snip>
   return 0;
}

Or of course you can pass it by C++ reference like:

void foo( char *& ptr)
{
   ptr = new char[255];
}

int mian()
{
   char *ptr = 0;
   foo( ptr );

   <snip>
   return 0;
}

based on these two methods is there any gain over one, is one safer than another?

The method I posted is from C language, while thekashap's method is c++. One is not safer than the other. Which method you choose would be a personal preference but in a c++ program you should probably use the c++ method for consistancy.

Just one addition, in case of passing by pointer you would be dereferencing the pointer to pointer everytime you use it. Although compilers may optimize it. A reference works as an alias.

thanks

How to pass char* as refrence in a funstion?

based on these two methods is there any gain over one, is one safer than another?

thanks

Seems like someone asked the question, some else read read the answers and followed up with more question and finally someone understood all and thanked. :)
If solved mark the thread as solved..

how to mark thread as solved

Seems like someone asked the question, some else read read the answers and followed up with more question and finally someone understood all and thanked. :)
If solved mark the thread as solved..

the information provided in this thread has been an awesome resource.

at first i was going huh? ** or *& but now it makes sense to me :-D


is a recomended read for anyone confused about pointers.

i read this site one nite and had somewhat of an epiphany lol

the site helped me to understand pointers and their usefullness.

As a rule of thumb, if you need to modify a variable in another function, you need to pass an address of it to the called function.

In this case you wanted to modify the char pointer and hence need to pass the pointer to the char pointer i.e. the address of the char pointer. References in C++ are just glorified pointers which act as the syntactic sugar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.