Hello! I am working on a small toolkit for a project I'm working on written in C for the TI-84 Plus CE. I'm using the library and toolchain provided by ce-programming.github.io. I have a definition file for the types and a function where I'm implementing the functions.

Here they are:
---
toolkit.h:

Code:

#ifndef TOOLKIT_H_INCLUDED
#define TOOLKIT_H_INCLUDED

#include <stdint.h>

/*
 *  Forward definition of all the structures declared in this file
 */

// Is this the face of god himself?
// Probably not.
typedef struct tk_widget;
typedef enum tk_widget_type;
// Elements
typedef struct tk_label;
typedef struct tk_image;
// Interactive
typedef struct tk_button;
// Layout
typedef struct tk_box;


typedef enum {
    TK_WIDGET_LABEL = 1,
    TK_WIDGET_IMAGE,
    TK_WIDGET_BUTTON,
    TK_WIDGET_BOX
} tk_widget_type;

typedef struct {
    tk_widget_type type;
    uint16_t desired_width;
    uint16_t desired_height;
} tk_widget;

/*
 * Generic template for a widget
typedef struct {
    tk_widget super;
    ...
} tk_...;

 */

typedef struct {
    tk_widget super;
    char* label;
} tk_label;

typedef struct {
    tk_widget super;

    uint8_t children_count;
    tk_widget** children;
   
    enum {
        HORIZONTAL = 1,
        VERTICAL
    } orientation;
} tk_box;

#endif // TOOLKIT_H_INCLUDED

---
tk_box.c

Code:

#include <stdlib.h>
#include <stdbool.h>

#include "toolkit.h"

tk_box* new_tk_box()
{
    // Allocate the memory for the new object
    tk_box *box = malloc(sizeof(tk_box));
    // set its type so that polymorphism can work
    box->super.type = TK_WIDGET_BOX;
    // set the child count to zero, as it has no children
    box->children_count = 0;
    // return it
    return box;
}

bool tk_box_add_child(tk_box *box, tk_widget *child)
{
    // Account for another child being added
    box->children_count++;
    // Attempt to expand the memory used by the pointers
    tk_widget** temp_pointer = realloc(box->children, sizeof(size_t) * box->children_count);
    // If it was valid, set our pointer to reflect the change
    if(temp_pointer != NULL)
    {
        box->children = temp_pointer;
        return true;
    }
    return false;
}

bool tk_box_remove_child(tk_box *box, tk_widget *child)
{
    uint8_t i;

    // Attempt to locate the child in our set of widgets
    for(i = 0; i < box->children_count; i++)
    {
        if(box->children[i] == child)
        {
            break;
        }
    }
   
    // If we didn't locate it, return false.
    if(i == box->children_count)
    {
        return false;
    }

    // Relocate the pointers after the child
    for(i = i; i < (box->children_count - 1); i++)
    {
        box->children[i] = box->children[i + 1];
    }

    // Attempt to shrink the memory block for our pointers
    tk_widget** new_pointer = realloc(box->children, sizeof(size_t) * box->children_count);

    if(new_pointer != NULL)
    {
        box->children = new_pointer;
        return true;
    }
    return false;
}

---
I'm trying to compile it with the eZ80 compiler provided for the TI-84 Plus CE. It's giving an error I don't quite understand:

Code:
C CE SDK Version 8.5
Z:\home\matthew\Projects\rpn\src\tk_box.c
Z:\HOME\MATTHEW\PROJECTS\RPN\SRC\TK_BOX.C   (23,15) :   ERROR (108) Identifier "tk_widget" is not a variable or enumeration constant name
Z:\HOME\MATTHEW\PROJECTS\RPN\SRC\TK_BOX.C   (23,31) :   ERROR (128) Identifier "temp_pointer" not defined within current scope
Z:\HOME\MATTHEW\PROJECTS\RPN\SRC\TK_BOX.C   (23,31) :   ERROR (100) Syntax error
make: *** [/opt/CEdev/include/.makefile:198: obj/tk_box.src] Error 255

Meanwhile, when I compile the same code with GCC, I get no such error. The only "error" is that the linker couldn't find main.

Any ideas what I'm doing wrong?
You need to define and initialize all the variables at the start of a function. i.e. move the declaration to the line above and it will work fine Smile (above the box->children_count++;
To clarify, you need to declare (not initialize!) at the start of scope (not function!). This is all standardized C89.
Ah, okay. I didn't understand you had to declare all the variables at the beginning of the scope they are used in. I'm used to C++ with GCC.

Thank you!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement