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

  • Home
  • SEARCH
  • 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 8706073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:31:28+00:00 2026-06-13T03:31:28+00:00

I am passing a char array by reference but when I return from function

  • 0

I am passing a char array by reference but when I return from function and print the array, it displays nothing. What am I doing wrong?

#include <iostream>

using namespace std;

void func(char []);
int main()
{
   char a[100];
   func(a);

   cout << a<<endl;
   return 0;
}

void func(char *array)
{
   array="Inserting data in array a";
   cout << array<<endl;
}

Regards

  • 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-06-13T03:31:29+00:00Added an answer on June 13, 2026 at 3:31 am

    You’re not passing the array by reference (nor should you, it will do you no good here). You are passing a pointer to its first element. You then reassign that pointer to point to something else inside the function. This has no effect on the array. If you want to change the contents of the array, then you need to copy data to the place that the pointer points to. You can use strcpy or similar for that:

    strcpy(array, "Inserting data in array a");
    

    As a side comment, but a very important one. We don’t need to deal with things like this in C++ anymore. That’s how you do things in C. Here’s how we do things in C++:

    #include <string>
    #include <iostream>
    
    void func(std::string & str)
    {
        str = "Inserting data into the string";
        std::cout << str << std::endl;
    }
    
    int main()
    {
        std::string a;
        func(a);
        std::cout << a << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What am I doing wrong here with passing a char array to a function
I am passing a char array ab[][] from serlet1 to ABC.jsp by: request.setAttribute(grid, ab);
I've read several discussions of passing char * in C. stackoverflow: passing-an-array-of-strings-as-parameter-to-a-function-in-c stackoverflow: how-does-an-array-of-pointers-to-pointers-work
I am having problems passing an array of strings to a function by reference
I'm currently passing a multidimensional char array to my MySQL function. cmd contains the
I have a question regarding passing a variable that is a char array from
I am using strcmp in following ways Passing char[] array names Passing pointers to
I'm passing an array of 76800 Booleans (could be integers or unsigned char* instead)
\u4000\f.TrimEnd(new char[0]) is equal to \u4000 . I am passing an empty array, so
Can anyone help me passing char array to method when i tried it is

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.