i suggest you to look into the differences between string and char*.
Essentially, any continuous stream of characters is a string. C implemented them using char*
- pointer to characters, AKA C-strings. These are null-terminated, such that the system knows when the end of string has been reached. C++ can also do that but it has its own string library which simplifies the usage compared to cstrings
Look into the link for in-depth explanation:
https://www.prismnet.com/~mcmahon/Notes/strings.html
UPDATE:
string
by itself is a c++ object data type, whereas char*
is simpy a pointer to a character (or continuous characters), which can be of variable length terminted by null characater \0
with implementation as such:
void printString(string& temp){
cout << temp << endl;
}
Your function call would be like this:
string text = "lorem ipsum";
printString(text);
This function implementation expects to get a reference to a string type as its input parameter. Hence, when i declare a string variable text
,i pass it on to the printString function by simply calling printString(text)
. The function call will take my input by reference. It does not create a copy of the temp
string within the function call
If your implementation is:
void printString(char* temp){
cout << temp << endl;
}
Then your function call would be:
char* text = "lorem ipsum";
printString(text);
This implementation takes a pointer as its input parameter and thus you have to pass a char*
to the function. Unlike the previous method where you could simply pass your string type as-is and it would be passed by reference.
Both these function calls print out "lorem ipsum"