How to pass char array values to another function using Call by value in C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to pass char array values to another function using Call by value in C++?

  1. #1
    Join Date
    Mar 2005
    Posts
    25

    Thumbs up How to pass char array values to another function using Call by value in C++?

    Hello all,
    I have tried to pass character array from main( ) function to getAddress () function using call by reference.But unforunately i have done some modification in that array.So original value was changed.Now I have to use call by value to pass my array from main () to getAddress.

    Is it possible to pass array using call by value in C++? or I have to put inside structure after that i have to pass structure to any function.

    kindly explain me using sample programs in C++.

    Regards
    R.Jaiganesh

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: How to pass char array values to another function using Call by value in C++?

    First of all, it would help if you posted some sample code illustrating your problem.

    However, have you tried declaring the argument const?

    Code:
    void func(const char* arg);
    that way, the called function can't change the contents of the char array. (If it needs to change them internally, but without reflecting that back to the caler, then you need to revisit your design.)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Mar 2005
    Posts
    25

    Re: How to pass char array values to another function using Call by value in C++?

    Hello,

    In my getAddress(),i have to change my value.I cannot change my design.Could you explain How to do passing char array using call by value in C++.

    Regards
    R.Jaiganesh

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: How to pass char array values to another function using Call by value in C++?

    You simply canīt. Arrays decay to pointers when passed to a function, so thereīs no way to pass them by value. You can either clone it manually before processing or you can use vector<char> and pass the vector by value.

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,140

    Re: How to pass char array values to another function using Call by value in C++?

    I would love to know why you can't pass a char* in to your function. Anyhow, as GNiewerth has said you can use vector<char> and you can use it as follows:

    Code:
    int main()
    {
      char* ca = "Hello";
      std::vector<char> cvec(&ca[0],&ca[5]); //copy the char array
      std::cout << &cvec[0] << std::endl; //print the char array stored by the vector.
    
      return 0;
    };
    The break down is as follows:
    Code:
      char* ca = "Hello";
    The above creates a char array with 6 elements (5 for Hello and 1 for the null terminator). The next thing to do is copy the char array to a vector<char>...
    Code:
      std::vector<char> cvec(&ca[0],&ca[5]);
    The above uses the vector range initialiser to copy the char array (this only requires one vector allocation and is the most efficient way to copy the char array into the vector). Note that all 6 elements are copied including the null terminator which is important if you are going to use the vector as you would a char array. Next, as an example to prove that it works, print the vector<char> as follows:
    Code:
      std::cout << &cvec[0] << std::endl;
    Basically, &cvec[0] references the first element in the array, and is also incidently the way to pass a vector through a C API. The output stream will stop printing once it hits the null terminator (without the null you would get buffer overrun which is really dangerous, hence why the null is required).
    Last edited by PredicateNormative; October 11th, 2007 at 03:59 PM. Reason: added explanation.

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: How to pass char array values to another function using Call by value in C++?

    Quote Originally Posted by jaiganeshbe
    Hello,

    In my getAddress(),i have to change my value.I cannot change my design.
    Then you've got bigger problems than the one you posted...
    Quote Originally Posted by jaiganeshbe
    Could you explain How to do passing char array using call by value in C++.
    As others have said, you can't. Either copy the contents of the string locally (use strdup()) or do the C++ thing and use a std::string, which you can pass by value.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,140

    Re: How to pass char array values to another function using Call by value in C++?

    Well, technically there is a way to do it (if you forgive the pass by reference instead of by value) but its very stupid, since someone is sure to call the function not realising the input char parameter is assumed to be the first element of a char array. Here is example code, but DON'T copy it, it is a stupid idea to the nth degree! Use a vector<char> if you have to pass by value (as already mentioned the example below is not actually pass by value, but pass by reference).

    Code:
    #include <iostream>
    #include <vector>
    
    void f(char &c)
    {
     char* array = &c;
     std::cout << array << std::endl;
    }
    
    int main()
    {
      char* cstr = "Hello";
      char& c = cstr[0];
      f(c);
    
      return 0;
    };

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center