• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

Passing string as a reference to a function.........

    How to pass a character array to a function using call-by-reference? I have a function which performs the operation as same as atof (converting string to float). When i passed the string as a reference, i got the following  error.

" error C2664: 'myatod' : cannot convert parameter 1 from 'char [9]' to 'char &'"

My program is given below.

using namespace std;
float myatod(char&);
main() {
      char str[]="-345.678";
      float k;
      k = myatod(str);
      cout<<"\n K: "<<k;
      return 1;

float myatod(char &s) {
   // Content of the sub routine

   Let me know where is the mistake.
  • 2
1 Solution
You function as shown above recieves a reference to a single character and not a character array, while u are actually passing a character array to it.

Arrays are always passed by reference [meaning of reference as per C].
i.e. Here Even if u say

float myatod ( char *s )

Then, only the base address [address of 0th location] of the passed array gets passed. Thus I don't think u need to pass it by C++ reference.


You can pass a reference to a pointer but that isn't needed in your case

You can do that using

float myatod ( char * &s )

Now  A refernce to a char * [i.e. a char pointer] is being passed.

This is useful if u need to modify the contents of the pointer variable itself. In your case, it won't be useful/applicable
Side note, not directly related to the question.

String literals are officially typed as const char*, and should be treated as such.
const char str[]="-345.678"; // better form

Thus the function declaration becomes:
float myatod(const char*);

Which kinda reads better, as atof doesn't modify the string parameter passed - I would expect your function myatod not to modify it either.
The yuckiness of the required declaration should set off alarm bells in your head and make you think, "should I be doing this?"

The only reason to pass the address of a pointer to a function is if you want to change the address pointed to by the pointer (i.e. make the pointer point elsewhere). If you pass the pointer by value you can still mess around with the data pointed to by the pointer.

You've probably got the idea of passing by reference because of vague ideas of passing std::string to functions. std::string is an object and may be passed by reference for optimum performance because it is big and because you probably don't want to play around with a copy of the object. A reference (which to all intents and purposes is a pointer) to the actual object means that less data is copied into automatic variables, when a function is called.

A copy of a pointer is more efficient to work with than a reference to a pointer, assuming you don't need to adjust what the address pointed to by the pointer. The pointer is already a "reference" (in the loose sense of the word) to an object, so ask yourself if you *really* want to pass a reference to the pointer. You certainly wouldn't expect to see references to references.

My guess is that you really want to prototype the function as...

   float myatod (const  char *s);

The only reason to use...

   float myatod( char* &s);

...is if (say) you want the adjust the pointer so that it points to the white space following your floating point number in your char buffer so you can keep on parsing it, and if that's your rationale, you might be interested in the standard library istringstream class:
#include <iostream>
#include <sstream>
#include <string>

int main()
        std::string line("100 123.456 Hello 99");
        std::istringstream isstr(line.c_str());

        int i;
        float f;
        std::string s;
        int j;

        isstr >> i >> f >> s >> j;

                << "i = " << i
                << ", f = " << f
                << ", s = \"" << s << '\"'
                << " and j = " << j
                << '\n';
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now