Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointers incorrectly become NULL #5

Open
jmanuel1 opened this issue Apr 11, 2014 · 0 comments
Open

Pointers incorrectly become NULL #5

jmanuel1 opened this issue Apr 11, 2014 · 0 comments

Comments

@jmanuel1
Copy link

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant