Functions

Hi there

I have another question which I think i am on the right track with, but i just want to make sure, if anyone is willing to share some knowledge.

I have the following code:
1) Am i right by saying that function ("blablabla", name, 888) for instance is not correct as you cannot take in a string literal ("") should rather take in a variable (string), and secondly i cannot assign a value (888).

Am i completely off the mark?

regards
Rome'
I'd need to see the actual function but as long as your function is expecting a string it shouldn't matter whether it's a variable or a literal.
It depends on how your function is declared and whether the string liiteral will be changed inside the function body.
If you have a function declared as
1
2
3
4
void function (std::string jibberish, std::string name, int number)
{
	// do something
}

You can call it with what you wrote just fine.
1
2
std::string name = "Peter";
function("blablabla", name, 888);
I don't know does anyone solved problem but i try to give my solution as far as i understand the problem......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstring>
using namespace std;

void a_function(const char * text1, string text2)
{
	cout << text1 << endl << text2 << endl;
}

int main(int argc, char ** argv)
{
	string a("Some fancy text");
	string b("Another fancy text");
	a_function(a.c_str(), b); // Note c_str() function
	return 0;
}
Topic archived. No new replies allowed.