Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:48:57+00:00 2026-05-26T05:48:57+00:00

I’m writing an extremely simple kernel for an embedded systems class. The board is

  • 0

I’m writing an extremely simple kernel for an embedded systems class. The board is the TI Stellaris EKI-LM3S8962. It runs C and has an OLED display. I am having issues with void pointers and de-referencing them. Any help is much appreciated.

My very small initial goal is to prove the concept of passing function pointers and struct pointers around. Is this the way that I access the var batteryState pointed to by the pointer batteryStatePtr which is part of the data struct passed in?

void status (void* taskDataPtr) {
    // make a temporary pointer to a Status Data Struct type
    SDS* data;
    // set data equal to the void* taskDataPtr now cast as a SDS pointer
    data = (SDS*)(taskDataPtr);
    (*data->batteryStatePtr)--;
    ...

Here is a very stripped down version of my code, the important region can be located by ctrl-f “HERE IS”

struct MyStruct {
     void (*taskPtr)(void*);
     void* taskDataPtr;
};
typedef struct MyStruct TCB;

The taskPtr points to a function that takes a void* as an arg and has a void* to a data struct. As a proof of concept I’m starting as small as possible. There are two functions, status and display.

typedef struct DisplayDataStruct {
    uint*  batteryStatePtr;
} DDS;
DDS DisplayData;

typedef struct StatusDataStruct {
    uint* batteryStatePtr;
} SDS;
SDS StatusData;

Status decrements the global variable batteryState through the taskDataPtr it is given. Display concatenates it onto a string and shows it on the OLED.

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

typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned short ushort;

void status (void* taskDataPtr);
void display (void* taskDataPtr);

void delay(uint msDelay);

//  Declare a TCB structure
struct MyStruct {
      void (*taskPtr)(void*);
      void* taskDataPtr;
};
typedef struct MyStruct TCB;

// status var
uint batteryState = 200;

typedef struct DisplayDataStruct {
    uint*  batteryStatePtr;
} DDS;
DDS DisplayData;

typedef struct StatusDataStruct {
    uint* batteryStatePtr;
} SDS;
SDS StatusData;

void main(void)
{
    DisplayData.batteryStatePtr = &batteryState;

    StatusData.batteryStatePtr = &batteryState;

      int i = 0;  //  queue index
      TCB* aTCBPtr;

      TCB* queue[2];

      TCB StatusTCB;
      TCB DisplayTCB;

      StatusTCB.taskPtr = status;
      StatusTCB.taskDataPtr = (void*)&StatusData;
      DisplayTCB.taskPtr = display;
      DisplayTCB.taskDataPtr = (void*)&DisplayData;

      // Initialize the task queue
      queue[0] = &StatusTCB;
      queue[1] = &DisplayTCB;

      // schedule and dispatch the tasks
      while(1)
      {
          for (i = 0; i < 2; i++) {
             aTCBPtr = queue[i];
             aTCBPtr->taskPtr( (void*)(aTCBPtr->taskDataPtr) );
          }
          systemState = (systemState + 1) % 100;
          delay(50);
      }
}

void status (void* taskDataPtr) {
    // return if systemState != 0 aka run once every 5 sec
    if (systemState) {
      return;
    }
    // make a temporary pointer to a Status Data Struct type
    SDS* data;
    // set data equal to the void* taskDataPtr now cast as a SDS pointer
    data = (SDS*)(taskDataPtr);


    // HERE IS where I am stumped. Is (*data->batteryStatePtr)-- the way you do this????
    // access the batteryStatePtr through the struct data
    // then dereference the whole thing to get at batteryState
    if ((*(data->batteryStatePtr)) > 0) {
      // decrement batteryState
      (*(data->batteryStatePtr))--;
    }
    return;
}

void display (void* taskDataPtr) {
    // run once every 5 sec
    if (systemState) {
      return;
    }
    DDS* data;
    data = (DDS*) taskDataPtr;
    char hold[12] = "Batt: ";
    char numHold[4];
    sprintf(numHold, "%u", (*(data->batteryStatePtr)));
    strcat(hold, numHold);

    // display the string hold
    RIT128x96x4StringDraw(hold, 15, 44, 15);
    return;
}

// use for loops to waste cycles, delay taken in ms
void delay(uint msDelay)
{
      // when i == 60000 and j == 100 function delays for ~ 7.6 sec
      msDelay = msDelay * 150 / 19;
      volatile unsigned long i = 0;
      volatile unsigned int j = 0;

      for (i = msDelay; i > 0; i--) {
             for (j = 0; j < 100; j++);
      }
      return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:48:57+00:00Added an answer on May 26, 2026 at 5:48 am

    The way of getting at the value batteryState through the void* structure is correct. For clarity, here it is:
    The data structure called data has a member called batteryStatusPtr. After getting the ptr, dereference it to get at what it is pointing at. Add more parenthesis for order of operations. (not 100% sure if those are necessary, but it allows me to sleep at night. Finally decrement it by 1.

    (*(data->batteryStatusPtr))--;
    

    In one line the problem was after using sprintf() to convert it to a string, and sending it to a display function, the value became corrupted with some strange garbage number. I don’t know why, but sprintf() was the cause. I wrote my own uint to string function and the problem was solved. The sprintf worked fine on my linux box, but the Texas Instruments LM3S8962 Lumninary Evaluation Board did not like it.

    Here is a stripped down, commented version of the code. As I’m a bit of a noob (in school, have a year of programming experience in Javascript, not C), I will take no responsibility for any errors. This is to the best of my knowledge. Perhaps in the process of stripping it is broken so I apologize in advance.

    // typedef uint as an unsigned integer for reference
    typedef unsigned int uint;
    
    // typedef TCB as a structure with two elements,
    // first a address of a function (actually not 100% sure about this one,
    //  i'm kinda a noob but I think that's right)
    // second a void pointer to a struct that stores data
    struct MyStruct {
        void (*taskPtr)(void*);
        void* taskDataPtr;
    };
    typedef struct MyStruct TCB;
    
    // declare a data struct SDS which holds data for the status function
    // there is only one variable in there, a uint* to the batteryState
    // called batteryStatePtr
    typedef struct StatusDataStruct {
        uint* batteryStatePtr;
    } SDS;
    
    // declare the global variable batteryState
    uint batteryState;
    
    // declare the function status that returns void and accepts a void* taskDataPtr
    void status (void* taskDataPtr);
    
    void main() {
    
        // declare a status TCB
        TCB StatusTCB;
        // declare a StatusDataStructure called StatusData
        SDS StatusData;
    
        // set the SDS task pointer to the address of the function status
        StatusTCB.taskPtr = &status;
        // set the SDS task data pointer to the address of the StatusDataStructure
        // which is cast as a void pointer
        StatusTCB.taskDataPtr = (void*)&StatusData;
    
        // declare a TCB pointer task and point it at the StatusTCB
        TCB* taskPtr = &StatusTCB;
    
        // call the status function through the TCBPtr and send it the dataStruct
        // associated with that TCB (StatusData)
        TCBPtr->taskPtr( (void*)(TCBPtr->taskDataPtr) );
    }
    
    void status (void* taskDataPtr) {
        // make a temporary pointer to a Status Data Struct type
        SDS* data;
        // set data equal to the void* taskDataPtr now cast as a SDS pointer
        data = (SDS*)(taskDataPtr);
        // access the global variable batteryState through the data struct associated
        // with the status TCB, of type SDS
        // decrement it if greater than 0
        if ((*(data->batteryStatePtr)) > 0) {
            (*(data->batteryStatePtr))--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.