CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Forward Reference to a Structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MMurray



Joined: 19 Feb 2011
Posts: 22

View user's profile Send private message

Forward Reference to a Structure
PostPosted: Thu Jan 29, 2015 6:39 am     Reply with quote

I read in a book somewhere that I should be able to do this...
Code:

//*PURPOSE Embedded C Programming Exercise 10_1

/* Linked List

*/

#include <e3.h>
#include <ios.h>

# define UNUSED 0xffff

typedef struct {
   int32 id_number;
   nodeptr *next;
} node;

typedef node * nodeptr;
node list[20];
int next_node=0;
nodeptr first = UNUSED;

void main(void){
 
   while(TRUE){
   // Enter the ID #
   // Add to the list
   // Last entry Next =UNUSED
   // Make the entry in numeric order
   }
}

but I cannot compile. The compiler complains that there is an unknown type in the structure (nodeptr *next). Is this just a compiler setting or did I miss something.

Matt
kWoody_uk



Joined: 29 Jan 2015
Posts: 47
Location: United Kingdom

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 7:23 am     Reply with quote

Hi,

You need to change your code to the following:-

Code:

# define NULL 0x00

// Defining a typename for a struct
// node_t is just a tag name, but is required
typedef struct node_t {
    int32 id_number;
    struct node_t * next_node;     // Must use struct keyword in front as compiler doesn't know what it is yet.
} node;                            // Node is the type name of the struct.

// Creating a new structure pointer, as yet uninitialised, i.e. pointing to nothing
node * newNode;

node list[20];
int next_node = 0;
node * first = NULL;

void main(void) {

    while (TRUE) {
        // Enter the ID #
        // Add to the list
        // Last entry Next = 0 or NULL
        // Make the entry in numeric order
        }
}


Linked lists are a diffcult concept to get your head round, especially as they contain pointers, which even "teachers" of C find difficult to explain/understand. Pointers are quite advanced.

Have fun.


Keith
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 7:48 am     Reply with quote

The key reason the original code fails, is that nodeptr is not defined when it is used.....
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 9:30 am     Reply with quote

Linked lists, in all their gloriously varied forms, are especially troublesome on PICs. Coding them is straightforward enough, but the very limited RAM available to be used as a heap from which to allocate memory dynamically means that its often difficult to make any such list hold enough to be useful.

Configuring the device for dynamic memory use is not automatic - it requires you, the programmer, to know what you are doing. Pointers in C and linked data structures in particular are prone to programming error, requiring careful and costly (time, effort and PIC resources) effort. Unlike a PC where you might get an error trap of some sort (the name of which has changed several times over the years), nothing like that exists on PICs - you are totally on your own. The first sign that something's gone wrong might well be a reset, with no hint at all of what caused it - not a great way to debug.

In many cases dynamic memory is not used on PICs, and such structures are therefore rarely used. In my a previous job, I wrote a programming style guide, and even though we were using ARMs with considerably more RAM than all but the biggest PICs, I explicitly banned dynamic memory use in our embedded code, such were the problems and pitfalls with using it. So, it should not come as surprise when I say that I have never used dynamic memory, and hence linked lists, on any PIC, and have never felt the loss of that. Not that they aren't a great programming tool, just that on PICs they are generally inappropriate, and that other tools, with fixed and controllable memory requirements, are safer and easier and a whole lot easier to debug.
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 10:15 am     Reply with quote

Very much agreed.

On the pointers, remember that in C, pointers can always be cast. The pointer is the same whether it is to an int8, or a 100byte structure. This is why languages often allow the pointer to be to something like 'void', and then when you want to use it you can cast it to be a pointer to the structure that was undefined when the pointer was declared.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group