Simple Explanation of string s(argc[1]);

Here's a sample snippet of code:

int main(int argv, char* argc[]) {
string s(argc[1]);
int i = atoi(argc[2]);

....

I would just like someone to clearly explain what the code " string s(argc[1]); " does and maybe provide some resources for further clarification.

Thanks!!
string s(argc[1]);

It says: make me a new object. I want this object to be of type string. I want this object to be named s. I want you to make this object using the constructor function that will accept argc[1] as a parameter.

We then look up, and see that argc[1] is a char*. So, this string object constructor function takes an object of type char*. We can look up this constructor function:

http://www.cplusplus.com/reference/string/string/string/

Aha, it's that one there: string ( const char * s );

We then read about that constructor function:
Content is initialized to a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of the character sequence is determined by the first occurrence of a null character (as determined by traits.length(s)). This version can be used to initialize a string object using a string literal constant.


Thanks for the quick reply Moschops! :)
Topic archived. No new replies allowed.