CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    27

    Passing String to a Function

    I know the following will look weird but I am simply not able to understand how string passing works.

    #include<iostream>
    #include<ctype.h>
    #include<fstream>
    using namespace std;

    void myfilemaker(char *fn);

    int main(void){
    char fn[20];
    cin>>fn;
    myfilemaker(&fn[0]);
    system("pause");
    }

    void myfilemaker(char &fn[0]){
    ofstream.myfile;
    myfile.open(fn[]);
    myfile<<"Something";
    myfile.close;
    return;
    }

    Please tell me how to pass the string. I have tried tweaking the fn variable a bit but nothing seems to work.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Passing String to a Function

    You don't need to treat fn as an array except at its declaration. Declare it as a char* in your function parameters, and just pass around fn, nothing more.

  3. #3
    Join Date
    Mar 2010
    Posts
    3

    Re: Passing String to a Function

    Hey, there. For starters, always make sure that your function prototype and the function definition have the same data type in their parameters (i.e. both are char*-types.) You had the right idea in the prototype, but in the function definition things got a little scrambled.

    For starters, when you make the ofstream "myFile", the syntax should be:

    ofstream myFile;

    not

    ofstream.myFile;

    Also, remember that the myFile.open() function takes a pointer to the string as a parameter. It looks like you were trying to pass the function the whole array, but that's overkill and it's improper syntax. Since your c-string is named "fn", the pointer to the c-string is simply "fn", so your myfilemaker() function should read:

    Code:
    void myfilemaker(char* fn)
    {
    ofstream myfile(fn);
    myfile<<"Something";
    myfile.close();
    }
    Also, just to get in the habit of good programmin', try to make your main() function return a value of 0, that's what generally symbolizes a successful execution. If you want to save some system resources, as will, use cin.get() instead of pausing the system, it's substantially less memory-intensive and performs the same function.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Passing String to a Function

    These two are not the same. I hope you can spot the difference.
    Code:
    void myfilemaker(char *fn);
    
    void myfilemaker(char &fn[0])
    I don't want to reiterate what was already said. Keep in mind that C-like strings, i.e. char*, are only a sequence of characters that ends with a null-termination character, or 0 (or '\0'). Though they are actually arrays or characters, they are called null-terminated strings. To work with such strings, you pass the address of the first character in the array. A function that takes a null-terminated string as input, actually takes a pointer to char*, because that is the type for the address of the first character in the array.

    On, the other hand, if you code in C++ you should use std::string class, and not care about the char*. http://www.codeguru.com/cpp/cpp/stri...cle.php/c13267
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Apr 2009
    Posts
    27

    Re: Passing String to a Function

    @CILU:

    Thanks! I will remember this one.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Passing String to a Function

    Quote Originally Posted by gameman144 View Post
    your myfilemaker() function should read:

    Code:
    void myfilemaker(char* fn)
    {
    ofstream myfile(fn);
    myfile<<"Something";
    myfile.close();
    }
    Note that since myfile is about to go out of scope anyway, the .close() call is redundant and unnecessary. The file will be closed when the ofstream's destructor is called.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Passing String to a Function

    Yes, but it's neither a harm, nor a bad practice.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured