Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Passing pointers to void that do not work

Status
Not open for further replies.

tebron

Member level 1
Joined
Dec 3, 2002
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
166
Hello,
I would like to submit my question to you experts of c since I am a beginner and I do not know how to solve the problem. I have a function that takes as a parameter a pointer to void, I have to pass a value of one or zero to turn on or off a led. I tried the code below, but, even if the compiler is free of errors in reality the value in the variable bLed remains always zero. Can you give me any guidance as to operate the routine? Thank you
very much.


main(void)
{
while(1)
{
TaskLedUno((BOOL*)1);
}
}

void TaskLedUno(void *pFlagLed)
{

BOOL bLed;

//while(1)
{

bLed=*(BOOL*)pFlagLed;
pLED_1(bLed);
}
}
 

Your problem stems from passing a pointer to a literal, most compilers will compile the code without error, however depending on the OS, if there is one, will throw a access violation upon running.

Try this code instead:

Code:
main(void)
{
    BOOL flag = (BOOL)1;
    
    // or
    // BOOL flag = TRUE; // if defined

    while(1)
    {
         TaskLedUno(&flag);
    }
}

void TaskLedUno(void *pFlagLed)
{

     BOOL bLed;

    //while(1)
    {

        bLed=*(BOOL*)pFlagLed;
        pLED_1(bLed);
    }
}

The code above compiles and runs without error and produces the desired result.

Ciao
 
  • Like
Reactions: tebron

    tebron

    Points: 2
    Helpful Answer Positive Rating
Hi,
Is there a way to pass a value of 1 or 0 without having to pass a variable?
 

Yes, pass it by value, not reference.

Code:
main(void)
{
    

    while(1)
    {
         TaskLedUno((BOOL)1);

         //or
         //TaskLedUno(TRUE);
    }
}

void TaskLedUno(BOOL FlagLed)
{

     BOOL bLed;

    //while(1)
    {

        bLed = FlagLed;
        pLED_1(bLed);
    }
}

If you try and pass an address of constant or literal, "& (BOOL)1," most compilers, will balk.

Besides, what is the point of having the routine, "void TaskLedUno(void *pFlagLed)," if you are going to force a static condition in your actual code?

You might as well as define two routines, "void LedOn(void)" and "void LedOff(void)."

Hope the info helps.

Ciao
 
Last edited:
  • Like
Reactions: tebron

    tebron

    Points: 2
    Helpful Answer Positive Rating
Why not just make it simpler?

void TaskLEDUno(Bool On);

If you want to turn on the LED, call TaskLEDUno(1) and to turn off the LED, call TaskLEDUno(0).
 

Why not just make it simpler?

void TaskLEDUno(Bool On);

If you want to turn on the LED, call TaskLEDUno(1) and to turn off the LED, call TaskLEDUno(0).

Yes. The pass by value example I posted previously, reference post #4, is exactly that function.

But, your correct SkyHigh, Tebron's program doesn't demonstrate a very prudent use of "void *."

However, not knowing Tebron's future plans for the "void TaskLedUno(void *pFlagLed)" routine, I was going to withhold my judgement concerning the use of "void *."

In the larger scheme of things, if Tebron were to later pass the "void TaskLedUno(void *pFlagLed)" as a function pointer in another routine, then it would make sense.
 

Yes. The pass by value example I posted previously, reference post #4, is exactly that function.

But, your correct SkyHigh, Tebron's program doesn't demonstrate a very prudent use of "void *."

However, not knowing Tebron's future plans for the "void TaskLedUno(void *pFlagLed)" routine, I was going to withhold my judgement concerning the use of "void *."

In the larger scheme of things, if Tebron were to later pass the "void TaskLedUno(void *pFlagLed)" as a function pointer in another routine, then it would make sense.


Hi,
I have a small RTOS and for create a task need to pass a pointer to void.

---------- Post added at 22:02 ---------- Previous post was at 21:57 ----------

Hi
I have a small RTOS and to create a task need to pass a pointer to void.
 

In the larger scheme of things, if Tebron were to later pass the "void TaskLedUno(void *pFlagLed)" as a function pointer in another routine, then it would make sense.

I thought that might be the case, therefore your use of "void *" is justified.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top