Working in C, I'm filling an array with char* return values from a function
char* files[4][12];
int i = 0;
for (;;)
{
char* file = get_value();
strcpy(files[i],file);
i++;
if (i > 4 || external_condition)
break;
}
// When I break out of
// my for loop the following
// code executes
for (i = 0; i < 5; i++)
{
if (files[i] != NULL)
manipulate(files[i]);
}
My problem is that if I break out of the first for loop without assigning values to all elements of files, my comparison in the second for loop fails. If only files[0] and files[1] have content, the loop processes files[2],file[3] and files[4] anyway.
strcpy(files[i],file);without allocating any memory for the pointerfiles[i]. This will result in undefined behavior.