"dbz" <pu**************@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Hello everyone. I have a query. Lets say that following is given:-
struct struct1{
char *word;
int n;
} *p;
QUERIES:
What does the following refer to?
1) p->word
2) (*p)->n
3) (*p)->word
4) *(p->word)
As others pointed out, this example suffers from:
1) unallocated structure
2) unitialized variables
3) undefined behavior
i.e., technically, all four will refer to garbage...
If we ignore all that, then using the first of the following C
transformations:
a->b (*a).b
a[b] *((a)+(b))
Given:
struct struct1
{
char *word;
int n;
} *p;
Then each of these "refer to:"
1) p->word
(*p).word
char *
(See warnings above.)
2) (*p)->n
(*(*p)).n
int
(It uses the some of the unallocated data that p may point to, as a pointer
to another unallocated structure with the same format as struct1, and then
refers to 'n' which is an 'int'. This, of course, assumes that the
uninitialized pointers aren't NULL by chance, and that you have access to
the memory region they point to, that the undefined behavior of accessing
them doesn't fail, and that the compiler you use doesn't detect this... See
warnings above.)
3) (*p)->word
(*(*p)).word
char *
(It uses the unallocated data that p points to, as a pointer to another
unallocated structure with the same format as struct1, and then refers to a
'word' which is a 'char *'. See warnings above and in 3). )
4) *(p->word)
*((*p).word)
*(char *)
char
(See warnings above.)
So, your answers should be:
1) p->word char *
2) (*p)->n int
3) (*p)->word char *
4) *(p->word) char
Rod Pemberton