Johs32 wrote On 03/02/06 16:27,:
I have a struct "my_struct" and a function that as argument takes a pointer
to this struct:
struct my_struct{
struct my_struct *new;
};
void my_func(struct my_struct *new);
I have read that there is no difference between giving this function a
pointer to a struct or the address of a struct:
1) passing a pointer to a struct:
struct my_struct *pp;
my_func(pp);
This is fine, provided `pp' has been given a valid
value before you try to pass it to my_func(). That is,
you must set `pp' to point to a `struct my_struct' object
or you must set it to NULL.
This is really no different than with non-pointer
variables:
double d;
your_func(d);
You must give `d' a valid `double' value before trying
to call the function.
2) passing an address of a struct
struct my_struct new;
my_func(&new);
This is fine. `struct my_struct new;' allocates
memory for the object, and `&new' produces a pointer
to that memory.
is it true that both of the above options are correct?
Yes (assuming a value is supplied in the first).
The difference is very much like the difference between
int variable = 6 * 7;
their_func(variable);
and
their_func(42);
Another thing I have been told that if I do:
struct my_struct *p;
p->new = NULL;
I should first malloc "p" because a pointer should be initialized before
using "->", but is that not what p->new = NULL does (initialising new) or
do I first need to malloc "p" before I initialize new?
You have confused yourself by using the name `new'
to mean three different things:
- It is the name of one of the elements contained
inside each `struct my_struct' object, and
- It is the name you have used for one such object, and
- It is the name of the formal parameter of the
my_func() function.
The name is the same, but the three things it names are
different. You may know several people called "Leo," a
name that is also used for one of the constellations and
for thirteen different Roman Catholic Popes. That doesn't
mean that your friend "Leo" is a collection of stars! I
suggest that you try to use different names for different
things, as far as is practical -- it will help to reduce
confusion.
You might also be confused by the fact that each
`struct my_struct' contains a pointer to a `struct my_struct'
object. There is nothing special about that inner pointer:
it's just a pointer. It could perfectly well be a pointer
to some completely different type of object -- it could just
as well be a `double'. The point (sorry) is that you cannot
get at that inner datum until you have a `struct my_struct'
that holds it. Backing up one level, you cannot use `p' to
get at that inner datum until you make sure `p' points to
a `struct my_struct'. You can do that by using malloc() to
obtain memory for the struct (and checking that it succeeded),
or by setting `p' equal to `& a_struct_my_struct_variable',
or by various other paths. In any event, you cannot use `p'
to read or write the content of the struct until you first
make sure `p' actually points to one.
--
Er*********@sun.com