0

The second if statement in main does not print out. If I put just one of the if statements they do execute, but for some reason putting them after each other causes the second if statement to not print anything. Can anyone explain why this happens?

#include <stdio.h>
#include <stdlib.h>

typedef struct entry {
    int value;        
    char *key;
    int type;
} entry;

entry* array[5];

void init_arr(){
    for(int i=0; i<5; i++){
        array[i] = NULL;
    }

    entry new;
    new.value = 2;
    new.key = NULL;
    new.type = 5;
    array[3]= &new;
        
}

int main(){
    init_arr();

    if(array[3]->value == 2){
        printf("ok\n");
    }

    if(array[3]->key == NULL){
        printf("ok 2\n");
    }
    return 0;
}```
4
  • I've just tried your code but it prints ok 2 Commented Dec 21, 2020 at 23:31
  • I think what you meant to write was entry array[5]; (an array of entry structs) rather than entry* array[5]; (an array of entry pointers)... that would have made the whole thing much easier... but if you want pointers, than you really don't want to to initialize the same pointer with stack memory (array[3]= &new) Commented Dec 21, 2020 at 23:32
  • that's weird, when i run it in visual studio code it only prints the first if statement Commented Dec 21, 2020 at 23:33
  • thanks @Myst , but i did want an array of entry pointers, i simplified this code but in my implementation i do indeed need an array of entry pointers. i just filled something random in array[3] to show that when i access different members of the same struct the second if statement doesnt execute Commented Dec 21, 2020 at 23:37

1 Answer 1

4

new is an automatic variable. It only exists while init_arr() is executing. As soon as you leave that function, the memory previously used for new may be used for some other thing, so both array[3]->key and array[3]->value are actually not defined.

If you get "right" values for array[3]->value is just because there is still a chance that memory used for new hasn't been already recycled by the time if first if is evaluated, but it has surely changed by the time the second if is evaluated.

If you need the memory used by new to be valid even after the function has exited, declare it static.

void init_arr()
{
    static entry new;

    for(int i=0; i<5; i++)
        array[i] = NULL;

    new.value = 2;
    new.key = NULL;
    new.type = 5;
    array[3]= &new;
}

Or allocate memory for it:

void init_arr()
{
    for(int i=0; i<5; i++)
        array[i] = NULL;

    array[3] = malloc(sizeof *array);     
    array[3].value = 2;
    array[3].key = NULL;
    array[3].type = 5;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.