Nitay
Nitay

Reputation: 4690

C Struct pointer issue

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?

Upvotes: 2

Views: 138

Answers (2)

pm007
pm007

Reputation: 383

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.

Upvotes: 1

iabdalkader
iabdalkader

Reputation: 17312

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++.

Upvotes: 2

Related Questions