-1

I noticed that you can declare a pointer to a struct in two ways:

struct spell * spells;

and

spell * spells;

what is the difference?

1 Answers1

2

In C, struct keyword must be used for declaring structure variables, but it is optional in C++.

For example, consider the following examples:

struct Foo
{
    int data;
    Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
    Foo a;  // Error in C, struct must be there. Works in C++
    return 0;
}

Example 2

struct Foo
{
    int data;
    struct Foo* temp;   // Works in both C and C++
};
int main()
{
    struct Foo a; // Works in both C and C++
    return 0;
}

In the above examples, temp is a data member that is a pointer to non-const Foo.

Jason
  • 36,170
  • 5
  • 26
  • 60