Question

I have the following array of wide characters:

wchar_t alphabet[] = L"abcdefghijklmnñopqrstuvwxyz"; /*  array of wide characters */

Everytime I try to run the following code, a "segmentation violation" error occurs

printf("%ls %d\n", alphabet, wcslen (alphabet));
int i;
for (i = 0; i <= wcslen (alphabet); i++)
{
    printf("%ls\n", alphabet[i]);
}

How can I correct this error?

Was it helpful?

Solution 2

Your loop is overstepping the array.

Arrays are indexed from 0, so the condition in a loop like yours should always test with <, never with <=.

Also, you're printing single characters with %s, which is also wrong. It should be %lc for a single wide character.

OTHER TIPS

"%ls" causes alphabet[i] to be interpreted as a pointer to a string. Use "%lc".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top