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?

5
  • 7
    Your problem might be somewhere else in the code causing this specific variable declaration to be parsed incorrectly. Commented Nov 19, 2012 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? Commented Nov 19, 2012 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? Commented Nov 19, 2012 at 19:09
  • Yes, before C99 I think, try moving them to the top of the function. Commented Nov 19, 2012 at 19:11
  • 1
    stackoverflow.com/questions/9903582/… Commented Nov 19, 2012 at 19:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

@RavindraBagale this is a keyword in C++
@Cicada it is still a possibility, no ? I've added the error from MSDN anyway
@mux:in question that was not a keyword, it showing because of SO's parser
@Cicada yes I get the syntax highlighting thing :) was just saying, anyway I removed that and just left the example
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.