Not able to use value of pointers

Fully featured PIC compilers available on Windows, Linux, and macOS.
Post Reply
Author
Message
Fiskelord
Posts: 8
Joined: 23 Nov 2016 09:46

Not able to use value of pointers

#1 Post by Fiskelord » 24 Jan 2021 16:09

Hello,

I have stumbled upon an error, and i cant for the life of me get it to work the i expect it to.

Here is my code:

Code: Select all

#include "board.h"

int *a = 3000;

void main(void)
{
    while (1)
    {
        if (*a > 2048)
        {
            //Do something
        }
        else
        {
            //Do something else
        }
    }
}
What im trying to do is to use the value of "a" to decide what to do, its hello world basic stuff. At least it should be :roll:

No matter what the value of "a" is, it will never hit "//Do something", it will always hit "//Do something else".

Now, im new to using pointers, but i believe you should get the value of the pointer, and not the address or some other weird output, by prefixing it with "*", right?
If not, and its just me being a complete noob, how do i use a pointer? I need to use pointers due to the analog_in_read function in MikroSDK

Im using Necto Studio 1.4.0, MikroC AI for PIC 1.1.0, MikroSDK 2.0.1, Windows 10 x64 20H2. The controller im programming is a PIC18F87K22, and im programming it using a MikroProg for PIC.

Thank in advance for the help :)

/Fiskelord

Fiskelord
Posts: 8
Joined: 23 Nov 2016 09:46

Re: Not able to use value of pointers

#2 Post by Fiskelord » 24 Jan 2021 18:25

UPDATE

Just ran this code through GNU's C compiler, CPP:

Code: Select all

#include <stdio.h>

void main(void)
{
    int a = 3000;
    int *b = &a;

    if (*b > 2048)
    {
        printf("True");
    }
    else
    {
        printf("False");
    }
}
... and that program worked as expected, if "a" is above 2048 it prints "True", otherwise "False".

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After i tried that, with success, i tried this slightly modified code back in MikroC AI:

Code: Select all

#include "board.h"

void main(void)
{
    int test = 3000;
    int *curr_skopi = &test;

    while (1)
    {
        if (*curr_skopi > 2048)
        {
            //Do something
            Delay_ms(1000);
        }
        else
        {
            //Do something else
            Delay_ms(1000);
        }
    }
}
...with breakpoints on the two delay's, and it always goes directly to the else brackets, i simply dont understand whats going on :cry:

/Jonas

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Not able to use value of pointers

#3 Post by filip.grujcic » 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,
Filip Grujcic

Fiskelord
Posts: 8
Joined: 23 Nov 2016 09:46

Re: Not able to use value of pointers

#4 Post by Fiskelord » 25 Jan 2021 13:00

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

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Not able to use value of pointers

#5 Post by filip.grujcic » 26 Jan 2021 14:14

Hello,

I was referring to the code from your original post. If you look at it you will see that it's different than the one in your second post. The code from the original post is not supposed to work as explained previously.

I believe the problem with your latest code comes from the fact that int a; is getting optimized. Try declaring it as static int.

Regards,
Filip Grujcic

Fiskelord
Posts: 8
Joined: 23 Nov 2016 09:46

Re: Not able to use value of pointers

#6 Post by Fiskelord » 08 Feb 2021 09:57

filip.grujcic wrote:
26 Jan 2021 14:14
Hello,

I was referring to the code from your original post. If you look at it you will see that it's different than the one in your second post. The code from the original post is not supposed to work as explained previously.

I believe the problem with your latest code comes from the fact that int a; is getting optimized. Try declaring it as static int.

Regards,
Hello again,

Yes, i know its different, i posted an update because i was aware of the error in my first one.

In any case, what solved it for me was using a normal variable instead of a pointer variable, unlike you showed would work in your documentation for the analog in. (By you, i dont mean you specifically, of course, but you as in MikroElektronika).

Thats where the problem started, and in an effort to find the source of the problem, i kept cutting lines away until only a few lines remained, and thats why i posted what i did.

I dont know man, you might not want to acknowlegde it, but something fishy is going on, i just cant for the life of me get the pointers to work as expected, but at least i found a solution to my problem. It would be really great to get an answer as to how pointers are used in your compiler though, since the usual way doesnt seem to work.

Post Reply

Return to “PIC AI compilers”