Array help

When I run this it displays some weird characters that shouldn't be there right after displaying the name. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  //prototype delarations
void displayString (char stringArray[]);

int main(int nNumberOfArgs, char* pszArgs[])
{
    char charMyName[] ={'N','a','m','e'};

    displayString (charMyName);

    system("PAUSE");
    return (0);
}

void displayString (char stringArray[])
{
    for (int i = 0; stringArray[i] != '\0'; i++)
    {
        cout << stringArray[i];
    }
}
You aren't null-terminating your string, so you're running right off the end of your array.
char charMyName[] ={'N','a','m','e','\0'};
Topic archived. No new replies allowed.