typedef structure issue

Share code examples or discuss embedded software, including device drivers, interrupt handlers, middleware and application code.

Moderators: Markus Girdland, Mattias Norlander

swsolutionsnv
Posts: 2
Joined: Sat Mar 30, 2019 6:48 pm

typedef structure issue

Postby swsolutionsnv » Thu May 23, 2019 8:00 pm

Code: Select all

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

typedef struct DisplaySegment
{
   unsigned char segmentId;
   bool bInFlash;
   unsigned int iValue;
       // linked list
   DisplaySegment *nextLinkedSegment;
} ;

I am attempting to setup a linked list.
however, the compiler is throwing an error for
DisplaySegment *nextLinkedSegment;

Unknown type name 'DisplaySegment'

I have built this code using a Microsoft compiler with no issues.

Is there some other include file needed or what?


Thanks in advance

bizulk
Posts: 5
Joined: Tue Mar 21, 2017 4:50 pm

Re: typedef structure issue

Postby bizulk » Fri May 24, 2019 7:29 am

typedef struct DisplaySegment
{
unsigned char segmentId;
bool bInFlash;
unsigned int iValue;
// linked list
struct DisplaySegment *nextLinkedSegment;
} MYSTRUCT ; <

rdamon
Posts: 10
Joined: Fri Mar 29, 2019 3:28 pm

Re: typedef structure issue

Postby rdamon » Fri May 24, 2019 5:00 pm

The basic issue is that the typedef name isn't visible until the struct definition is complete, so the struct definition can't use the typedef name.
The struct tag name is available as soon as you start the definition, so you can use it in the definition. In addition, you didn't provide the name that the typedef defined, which was presumable also supposed to be DisplaySegment.

Microsoft accepted the code, because it is really a C++ compiler, and in C++ a struct definition creates an implied typedef of the struct tag to the same name, and in C++ that typedef happens at the beginning of the struct definition.

In C, you need to refer to the type with the struct tag inside the recursive definition or do something like:

typedef struct DisplaySegment DisplaySegment;

struct DisplaySegment {
...
DisplaySegment* nextLinkedSegment;
};

Note, creating the typedef before the definition of the struct.

swsolutionsnv
Posts: 2
Joined: Sat Mar 30, 2019 6:48 pm

Re: typedef structure issue

Postby swsolutionsnv » Wed May 29, 2019 4:37 pm

To both the respondents:

THANK YOU VERY MUCH :!:

with both explanations, I have been able to move on with my port.

When I learned C, it was the basics; then moved to C++ (via MS).
It seems as if their compiler spoiled me. :mrgreen:


Return to “Embedded target code development discussions”

Who is online

Users browsing this forum: No registered users and 0 guests