Вопрос

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?

Это было полезно?

Решение 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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top