2

I'm constructing a linked-list, and this is the list item Struct:

struct TListItemStruct
{
    void* Value;
    struct TListItemStruct* NextItem;
    struct TListItemStruct* PrevItem;
};
typedef struct TListItemStruct TListItem;
typedef TListItem* PListItem;

I use this in several function and it looks ok so far. However when I define the following variable:

PListItem item;

I get the following error:

error C2275: 'PListItem' : illegal use of this type as an expression

Why is that? What's wrong with defining a variable of type pointer to struct?

EDITS: This is more of the function. This doesn't work

BOOL RemoveItem(PListItem item)
{
    // Verify
    if (item == NULL)
    {
        return FALSE;
    }
    // Get prev and next items
    PListItem prev;
    prev = item->PrevItem;
    //PListItem next = (PListItem)(item->NextItem);
 ...

However this works:

BOOL RemoveItem(PListItem item)
{
    PListItem prev;
    // Verify
    if (item == NULL)
    {
        return FALSE;
    }
    // Get prev and next items
    prev = item->PrevItem;
    //PListItem next = (PListItem)(item->NextItem);
 ...

I'm using VS2012, maybe it's a standard thing? to declare vars in the beginning of the function?

Nitay
  • 4,193
  • 6
  • 33
  • 42
  • 7
    Your problem might be somewhere else in the code causing this specific variable declaration to be parsed incorrectly. – Jason Nov 19 '12 at 19:00
  • This is a C, C++ problem. The pointer "this" doesn't exist on C, which compiler are you using? What extension is your file? Can you show some more code? – imreal Nov 19 '12 at 19:04
  • Seems like it's because I've declared the vars in the function body and not right on start. Is this a C issue? – Nitay Nov 19 '12 at 19:09
  • Yes, before C99 I think, try moving them to the top of the function. – imreal Nov 19 '12 at 19:11
  • 1
    http://stackoverflow.com/questions/9903582/error-c2275-illegal-use-of-this-type-as-an-expression – imreal Nov 19 '12 at 19:18

2 Answers2

2

MSVC uses C89, it does not support C99, so you need to either declare all variables at the beginning of your function or compile as C++.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
1

In C89 (which is supported by Visual Studio 2012) you have to declare variables at the beginning of the scope. That's why your latter example works fine.

pm007
  • 363
  • 1
  • 9