In C++, the ampersand symbol
& has three possible meanings depending on the context:
1) Reference declaration/parameter.
2) Address-of operator. (This is what you have now).
3) Bitwise And operator.
The address-of operator, also known as reference operator, takes the memory address of something.
If we look at the declaration of
std::strncmp() we see that the first parameter is a pointer to
char.
int strncmp ( const char * str1, const char * str2, size_t num ); |
http://www.cplusplus.com/reference/cstring/strncmp/
A pointer is a variable containing a memory address.
However, by passing simply
s1[i] we would pass a
char, and not a pointer to
char.
So we prefix it with an ampersand to take its memory address:
&s1[i].
To reiterate,
s1[i] is the character at position (index)
i in string
s1.
While
&s1[i] is the memory address of the character at position (index)
i in string
s1.