LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2003, 09:07 PM   #1
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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&)'

Last edited by jkobrien; 11-05-2003 at 08:40 AM.
 
Old 11-01-2003, 09:29 PM   #2
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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.
 
Old 11-01-2003, 09:30 PM   #3
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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]

Last edited by jhorvath; 11-01-2003 at 09:34 PM.
 
Old 11-01-2003, 09:30 PM   #4
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
replace int dash_to_underscore(char &abcde) with int dash_to_underscore(char abcde) and that should work.
 
Old 11-01-2003, 11:32 PM   #5
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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

Last edited by jhorvath; 11-01-2003 at 11:42 PM.
 
Old 11-04-2003, 04:57 PM   #6
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Original Poster
Rep: Reputation: 30
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

Last edited by jkobrien; 11-05-2003 at 08:42 AM.
 
Old 11-04-2003, 10:49 PM   #7
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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
 
Old 11-05-2003, 02:50 AM   #8
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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..
 
Old 11-05-2003, 01:41 PM   #9
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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,

Last edited by jhorvath; 11-05-2003 at 03:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem passing string::size_type by reference (C++) timhardy Programming 2 04-24-2005 05:14 PM
passing structures to function b123coder Programming 3 04-20-2005 10:15 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration