Send array of char to a function

Hello,

what is the difference between these? (W and &W[20] in line 15)
When I use &W[20] output is wrong.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

void Palindrome(char word[])
{
	cout << word;
}

int main()
{
	char W[20];
	cout << "Enter the word: " << endl;
	cin >> W;
	Palindrome(W);
	return 0;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

void Palindrome(char word[])
{
	cout << word;
}

int main()
{
	char W[20];
	cout << "Enter the word: " << endl;
	cin >> W;
	Palindrome(&W[20]);
	return 0;

}
Last edited on
Well
Palindrome(W);
is the same as
Palindrome(&W[0]);

In the former case, it's pointing to the start of the word.

In the latter, you're pointing outside the array.
Last edited on
Topic archived. No new replies allowed.