I know how the strtok() function works in C/C++. But the following function (in which I have used strtok() function) is showing unexpected behaviour .

```
void stringtokens(char a[][1000], int n, int key)
{
char tokens[n][1000];
for (int i = 0; i < n; ++i)
{
int col = 1;
char* ptr = strtok(a[i], " ");
while (col != key)
{
ptr = strtok(NULL, " ");
++col;
}
for (int j = 0; j < strlen(ptr) + 1; ++j)
{
tokens[i][j] = ptr[j];
}
}
}
```

When I used the above function in my following code –

```
int main()
{
int n;
cin >> n;
cin.get();
char a[n][1000];
for (int i = 0; i < n; ++i)
{
cin.getline(a[i], 1000);
}
int key;
cin >> key;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < strlen(a[i]); ++j)
{
cout << a[i][j];
}
cout << endl;
}
stringtokens(a, n, key);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < strlen(a[i]); ++j)
{
cout << a[i][j];
}
cout << endl;
}
}
```

The 2-d character array a[n][1000] got altered somehow… which gave different outputs in the two cases, before execution of stringtokens() and after execution of stringtokens().

The input and output were –

```
3
92 022
82 12
77 13
2
92 022
82 12
77 13
92
82
77
```

Please somebody help me out to understand the above behaviour of the function and the unexpected output that I have got. By the way thanks for helping me in advance.

Source: Windows Questions C++