filip.grujcic wrote: ↑25 Jan 2021 12:37
Hello,
The code you ran in mikroC AI is not the same as the one you ran in an online compiler.
During pointer initialization the address is assigned, not value, therefore: int *a = 3000;
means that "a" points to the 3000 memory address.
Your if condition depends on what is actually in the memory on that address because of the dereference operator. If you are using the simulator compiler will assume the value is 0, therefore your condition will never be true.
The code you ran in the online compiler is different, because there you assigned the address of "a" variable to the "b" pointer, which is correct and therefore your if condition is met.
Regards,
Hello Filip, and thanks for the answer.
I dont understand how the two snippets of code in my "UPDATE" post is different, when it comes to the pointer.
The first snippet, the one with "#include <stdio.h>" as the first line, is the one CPP would compile, and that worked as expected if i changed the value of "a".
The second snippet, in the same "UPDATE" post, starting with "#include "board.h"", the only difference is the variable names and the fact the if statement is inside a while loop. Both of them initialize an integer "test" as 3000, initialize a pointer pointing to "test" and comparing the value of the pointer to a constant.
Problem is, they dont do the same after compilation, even though they should. They should both change what bracket to execute, according to the pointer, but MikroC AI doesnt. And none of them is simulated, both snipptes are run on real hardware, though the CPP snippet is run real time, and MikroC AI is run with a debugger.
For reference, here is the two snippets again, with variable names changed to match each others and while loop removed for the MikroC AI snippet:
The one for CPP:
Code: Select all
#include <stdio.h>
void main(void)
{
int a = 3000;
int *b = &a;
if (*b > 2048)
{
printf("True");
}
else
{
printf("False");
}
}
The one for MikroC AI:
Code: Select all
#include "board.h"
void main(void)
{
int a = 3000;
int *b = &a;
if (*b > 2048)
{
//Do something
Delay_ms(1000);
}
else
{
//Do something else
Delay_ms(1000);
}
}
If they really are different, can you point out how? I just tried them again, same result, i just dont understand what i am missing here.
/Jonas