JS said:
Is it only allowed to make a pointer point to something in a method??
#include <stdio.h>
struct data {
int x;
int y;
};
struct data test, *pp;
pp = &test; //WHY IS THIS ILLEGAL??
Because it is an executable statement, and an executable
statement must reside inside a function (not "method").
This assignment is illegal for the same reason an exit()
call or a `for' loop would be illegal at this point.
However, you can still achieve your goal by using an
initializer in the declaration of `pp':
struct data test, *pp = &test;
or perhaps more clearly
struct data test;
struct data *pp = &test;
Despite the presence of the `=' this is *not* an assignment
statement; it is a declaration with an initializer. Keep in
mind that C tends to re-use the same character for multiple
purposes. Depending on context, `*' can be the multiplication
operator or the pointer dereference operator or part of a `/*'
or `*/' comment marker or part of the `*=' multiply-and-assign
operator. Just so, `=' can be the assignment operator, or part
of the syntax of an initialization, or part of `==' or `!=' or
`*=' or ... Despite the fact that they both use `=' and have
the same set of symbols on both sides of it, your code and mine
are different: one is an executable statement, and the other
is a variable declaration with an initializer.