7 years ago.

Pointer to pointer practice

Hi guy, I am practicing pointer to pointer by C. I use the thread sample to modify below, but the LED1 is always turn on. Could anyone tell me how to accomplish this?

Pointer to pointer

#include "mbed.h"
#include "rtos.h"

Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;

void ph2(DigitalOut ***y)
{
        ***y = 1;
        wait(0.5);
        ***y = 0;
}    

void ph1(DigitalOut *x)
{
        DigitalOut **ptr = &x; //this declaration is necessary.
    
        ph2(&ptr);
} 
// Blink function toggles the led in a long running loop
void blink() {
    while (running) 
    {
        ph1(&led1);
    }
}
// Spawns a thread to run blink for 5 seconds
int main() {
    thread.start(blink);
    wait(8);
    running = false;
    thread.join();
}

1 Answer

6 years, 11 months ago.

Hello Terry,

Try the following code:

#include "mbed.h"
#include "rtos.h"
 
Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;
 
void ph2(DigitalOut ***y)
{
        ***y = 1;
        wait(0.5);
        ***y = 0;
        wait(0.5);    // this line was missing
}    
 
void ph1(DigitalOut *x)
{
        DigitalOut **ptr = &x; //this declaration is necessary.
    
        ph2(&ptr);
} 
// Blink function toggles the led in a long running loop
void blink() {
    while (running) 
    {
        ph1(&led1);
    }
}
// Spawns a thread to run blink for 5 seconds
int main() {
    thread.start(blink);
    wait(8);
    running = false;
    thread.join();
}

Accepted Answer

Wonderful!! It's work!

posted by Terry Lung 13 Apr 2017