lcc
lcc copied to clipboard
Pointers incorrectly become NULL
Lcc will compile this code incorrectly, making the pointer in the struct iter_obj passed to iterate become NULL when it really isn't supposed too.
#include <stdio.h>
struct iter_obj
{
void *iterable;
};
#define new_iter_obj(iter) {iter}
void *array_item(int index, void *iterable)
{
if (index < 10)
{
return (void *) &(((int *) iterable)[index]);
}
else
{
return NULL;
}
}
void iterate(struct iter_obj *obj)
{
void *tmp;
int index = 0;
/* obj->iterable == NULL, but it should point to countdown in main */
while ((tmp = array_item(index, obj->iterable)) != NULL)
{
printf("%d\n", *((int *) tmp));
index++;
}
}
int main()
{
int countdown[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
/* cd_iter.iterable == countdown */
struct iter_obj cd_iter = new_iter_obj((void *) countdown);
iterate(&cd_iter);
}