Thread: Really simple references/char question

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    11

    Really simple references/char question

    Hi, little problem with references, char*s and passing.

    To simplify the problem:
    Code:
    #include <iostream.h>
    
    char& foo(char& c, const char& d) {
      cout << c << " " << d << endl; 
      c = d;
      return c;
    }
    
    main() {
      char* c = "This is a test";
      char* d = "This is also a test"
      
      foo(c,d);
    
      cout << c;
    }
    This doesn't compile. I could pass foo(*c,*d), but then foo() will only have access to the first character.

    Insight?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You could do it like this. Pass the addresses of the strings and the function can use the strings.

    Code:
    char *foo(char *c, char *d) {
      cout << c << " " << d << endl; 
      c = d;
      return c;
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    I don't suppose there's a way of doing that by using references in the argument list?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    To do this the "C++" way you should use std::string instead of char *.

    Something like this..
    Code:
    #include <iostream> //DO NOT USE .h headers
    #include <string>
    using namespace std;
    
    
    //Note void. No need to return since you are using a reference
    void foo(string &c, const string &d) 
    {
      cout << c << " " << d << endl;
      c = d;
    }
    
    int main()
    {
      string c = "This is a test";
      string d = "This is also a test";
      foo(c, d);
      cout << c << endl;
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    I suppose I was trying to mix to many different styles there. Thanks for the help.

    (by the way, I was returning char& so I could say something like cout << foo(a,b);)

    Does it really matter if you include the .h?

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    .h is non standard

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    but then foo() will only have access to the first character.
    Well... not true!

    Arrays are stored sequentially, so if your function can get-to the first character, it can get to 'em all. If fact, passing a pointer is the most common way of doing this sort of thing. (And the most common way of getting a function to update multiple variables.)

    If you're a "newbie", this may not mean much yet, but pointers are also used to pass structures and objects into functions.

    To do this the "C++" way
    mousey,
    FYI - Many books and instructors teach the "C way" first. (aka C-style strings, null-terminated strings, or character arrays.) So, you can hold-off on using <string> if that's the way your class/book is structured.

    [EDIT] The Windows API uses C-style strings too. Don't 'ya just love it when they teach you the hard way first!
    Last edited by DougDbug; 10-16-2003 at 12:27 PM.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    Originally posted by DougDbug
    If you're a "newbie", this may not mean much yet, but pointers are also used to pass structures and objects into functions.
    Lol, I guess you could say I'm a newbie C++ programmer, but I'm experienced in Pascal and Basic (to an extent), and I've programmed in C before, so I know the basics .
    Arrays are stored sequentially, so if your function can get-to the first character, it can get to 'em all. If fact, passing a pointer is the most common way of doing this sort of thing. (And the most common way of getting a function to update multiple variables.)
    So I could actually iterate through the string after passing it? I didn't actually think of this, as soon as reading the pointer only revealed the first character, I gave up .

    Thanks for the info.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    If you use foo(*c, *d) as you mentioned and then take the address of your parameters in foo(), you will get the c-string as you would expect.

    Code:
    #include <iostream>
    
    void foo(char& c)
    {
      std::cout << &c << std::endl;
    }
    
    int main()
    {
      char* c = "hello world";
      foo(*c);
    }

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    Thanks, that's really useful

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use * or [] when refering to C style strings. I can't think of an appropriate use for & when it comes to C style strings----though I suspect somebody may prove me wrong very shortly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM