LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing a string to a function (https://www.linuxquestions.org/questions/programming-9/passing-a-string-to-a-function-111298/)

jkobrien 11-01-2003 09:07 PM

passing a string to a function
 
Hi,

I'm working my way through a book on C++ programming and am stuck on an exercise on passing a string to a function to have underscores replaced by hyphens, but I can't even pass a string to the function.

I can't get past this exercise, can someone please tell me what is wrong with this program?


#include <iostream>
#include <string>
using namespace std;
char abcde[30];


int dash_to_underscore(char &abcde)
{
cout << "string (abcde) " << abcde << '\n';
return(0);
}

main()
{
strcpy(abcde, "ab-asdf_asdf-oij");
dash_to_underscore(abcde);
return (0);
}


This is the error I get...

stupid.cc: In function `int main()':
stupid.cc:16: could not convert `&abcde' to `char&'
stupid.cc:8: in passing argument 1 of `int dash_to_underscore(char&)'

mr_segfault 11-01-2003 09:29 PM

I'd either:

Code:


#include <iostream>
#include <string>

int dash_to_underscore(std::string &theString)
{
  std::cout << "theString  =  " << theString << std::endl;
  return 0;
}

main()
{
  std::string myString("ab-asdf_asdf-oij");

// look up operations on std::string (like find_first_of etc)

  dash_to_underscore(myString);
  return 0;
}

or


Code:


#include <iostream>

int dash_to_underscore(char *theString)
{
  std::cout << "theString  =  " << theString << std::endl;
  return 0;
}

main()
{
  char myString[] = "ab-asdf_asdf-oij";
  dash_to_underscore(myString);
  return 0;
}


Hope that helps.

jhorvath 11-01-2003 09:30 PM

you're trying to pass a pointer (arrays are just fancy pointers).

with the call-by-reference method, you pass 'addresses' that can be modified by the function. what ever you changes you make to that object that was passed, is changed outside the function as well. essentially you are passing a pointer and are asking the function to use the address of an address :) ..you can change your function definition to

int dash_to_underscore(char * string)

and either pass the array you have...or you can use a char *, or even just pass the address of (&) a plain 'char'

[EDIT]
...sorry i was in the middle of posting :)
[/EDIT]

[EDIT AGAIN]

also change your 'main()' line to 'int main()

[/EDIT AGAIN]

vanquisher 11-01-2003 09:30 PM

replace int dash_to_underscore(char &abcde) with int dash_to_underscore(char abcde) and that should work.

jhorvath 11-01-2003 11:32 PM

Quote:

asking the function to use the address of an address
...sorry, i think i confused even myself with that statement :) ..because a pointer (in which the contents consist of an address), does infact, have its *own* address.

i think what i meant was...that the function, after receiving the object..then tries to manipulate that object using it's address. in the event that you should pass the pointer to that same function... first off ..it fails because a pointer of some_type is not a reference of some_type, in which case we have a type mismatch.. and second, if it *could* receive a pointer ...with its current implementation, the compiler would substitute that pointer with it's address which has stored in it, another address (for instance 0x1d03975 ...or whatever :) ..in which case, i believe we would be left manipulating that '0x1d03975' value ...in essence pointing the pointer to another area in memory (or just storing some illegal garbage value ), ...either way we destroy the pointer and/or just crash the prog.

...i think :)
i'm still learning all this myself...

if someone can correct anything that is wrong or *way* off with what i just said ...please do ...i would much enjoy *knowing* what is going on at all times....

thanks, and sorry if i confused anyone

jkobrien 11-04-2003 04:57 PM

Many thanks for the help, guys. Hope you don't mind me tacking on another couple of questions.

The book I'm using is from 1997. It seems that there has been some changes to C++ since then (e.g how the standard namespace is used and declaring iostream rather than iostream.h). I was thinking I could still work through the book I have and pick up on the changes as I go or afterwards. Do you think this is reasonable, or would I be better off ditching my book and getting a more up-to-date one?

Secondly, and maybe I'm jumping too far ahead of myself, but how can I tell what functions a class gives me (e.g. what else does iostream give me). And, coming from the other direction, if I need a function how can I find out what class it's in?

Thanks again,

John

jhorvath 11-04-2003 10:49 PM

1: ...the only thing that getting a new book would hurt, is your wallet :)

2: get the source code for the c++ std library ..should've come with your distro on cd., or goto step one (above) ...give in ...get a book that covers that stuff in detail ..i have c++ programming langauge specail edition (stroustroup might as well have written the damn thing in russian) ...it's a good reference though ...also someone mentioned some ANSI/ISO c++ book , that may go into detail about that stuff ...but as far as 'browsing' the implementation is concerned, you better off just checking out the headers and source files i think ...IMO that is

mr_segfault 11-05-2003 02:50 AM

As for reference material,

The roguewave site has a nice STL reference: STL reference alpha listing

And the use guide: Userguide

And Bruce Eckel has his good :Thinking in C++

The books that I would recommend are:
The last two books I believe are two of the best books to have at hand for a C++ programmer.

The C++ FAQ is a very easy to read very informative book. The other covers almost the entire standard, but in a manner which is both readable and understandable by normal humans :) It covers the implementation details to great detail.

Cheers..

jhorvath 11-05-2003 01:41 PM

i'm going to go out on a whim and trust your judgement ..:)

i just ordered them :) ...it's a good thing you posted those links..i had a book that i was going to get on 65816 assembly (super nintendo(65c816) baby...OHHHH YEAH!!.) ..anyways , it was sitting in my shopping cart for like seven months (i forgot about it :), but i picked that up too. (that book is 'Programming the 65816: Including the 6502, 65C02, and 65802' for those that are interested in learning about the GREATEST SYSTEM MAN HAS EVER CONCEIVED!!! :) ...also useful for c64, and the atari used this processor family as well :) any nostalgia phreaks?

the book i got..'C++ Programming Langauge: Special Edition', isn't a baaad book persay. but it's not one of those books that you read from cover to cover, which is where i run into trouble. :) ,but i'm at the point now where i just try to get something done, and look through the book when i get stuck, and also for 'better' ways of doing something (like dakensta had posted in another post, using a multimap<>, which i found pretty cool)
either way...the more references the better i say.

so thanks again, especially for inadvertantly reminding me about my 'other' long lost book (it was printed in '79 and i've only seen it on amazon).

cool,


All times are GMT -5. The time now is 08:01 PM.