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?
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.