user14489561
user14489561

Reputation: 29

Unexpected expression in C

May I know why I am getting an Error:

Unexpected Expression before 'INTL_MONEY_VALUE_PTR'?

I want to create a data type INTL_MONEY_VALUE which can store a floating-point number to represent a money value and currency. Also, INTL_MONEY_VALUE_PTR, being a pointer to the international money value type above.

#include <stdio.h>
#include <string.h>
#define COUNTRY 4
#define AMOUNT 20.20


int main()
{
    typedef struct
    {
        float value;
        char currency[COUNTRY];
    } INTL_MONEY_VALUE;

    INTL_MONEY_VALUE sg = { AMOUNT, "SGD" };

    // Pointer
    typedef INTL_MONEY_VALUE *INTL_MONEY_VALUE_PTR;

    printf("Amount: %s %.2f\n", sg.currency, sg.value);
    printf("Pointer: %s\n", *INTL_MONEY_VALUE_PTR);

    return 0;
}

Upvotes: 0

Views: 181

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155536

INTL_MONEY_VALUE_PTR is a typedef, not an actual declared pointer variable. It doesn't even exist at runtime (it's just a convenience feature for declaring pointers to INTL_MONEY_VALUE). You can't dereference it, because "it" doesn't exist.

If you want to declare a pointer to INTL_MONEY_VALUE, you wouldn't use typedef, just:

INTL_MONEY_VALUE *INTL_MONEY_VALUE_PTR;  // possibly initialized to &sg

If you wanted to print the address of the actual declared instance of the struct (which is what the code seems to indicate it should do), you don't need the typedef or a declaration at all, you'd just do:

printf("Pointer: %p\n", &sg);

Upvotes: 2

Related Questions