By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 1,980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

A Query in "Pointers to Structures"

P: n/a
dbz
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)

Jul 15 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"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)
1) p->word == (*p).word == adress of first element of char array.

2) That would be illegal indirection since "p" is not a double pointer, but
if it is than
(*p)->n == (*(*p)).n == number stored in variable n.

3) --2) == adress of first element of char array.

4) First element of char array.
Jul 15 '06 #2

P: n/a
dbz wrote:
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?
Homework.
1) p->word
2) (*p)->n
3) (*p)->word
4) *(p->word)

August
Jul 15 '06 #3

P: n/a
dbz wrote:
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)
Nothing yet. You have declared a structure and defined a pointer to it,
but you don't have a structure yet.

p = malloc(sizeof *p);

Now, assuming success of malloc, p points to a structure and p[0] is the
structure.

1. p->word is just fine.
2. p->n or (*p).n or p[0].n
3. (*p).word or p[0].word
4. *(p->word) is just fine.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 15 '06 #4

P: n/a
Joe Wright wrote:
dbz wrote:
>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)
Nothing yet. You have declared a structure and defined a pointer to it,
but you don't have a structure yet.

p = malloc(sizeof *p);

Now, assuming success of malloc, p points to a structure and p[0] is the
structure.

1. p->word is just fine.
2. p->n or (*p).n or p[0].n
3. (*p).word or p[0].word
4. *(p->word) is just fine.
Don't all four yield undefined behavior for the
use of indeterminate values?

--
Eric Sosman
es*****@acm-dot-org.invalid

Jul 15 '06 #5

P: n/a
Tosha a écrit :
"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)


1) p->word == (*p).word == adress of first element of char array.

2) That would be illegal indirection since "p" is not a double pointer, but
if it is than
(*p)->n == (*(*p)).n == number stored in variable n.

3) --2) == adress of first element of char array.

4) First element of char array.

Why do you do somebody else's homework?

You are not really helping him, besides,
all the answers are completely false!

jacob
Jul 15 '06 #6

P: n/a

"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
Jul 15 '06 #7

P: n/a

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:44*********************@news.orange.fr...
Tosha a écrit :
>"dbz" <pu**************@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegr oups.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)


1) p->word == (*p).word == adress of first element of char array.

2) That would be illegal indirection since "p" is not a double pointer,
but if it is than
(*p)->n == (*(*p)).n == number stored in variable n.

3) --2) == adress of first element of char array.

4) First element of char array.

Why do you do somebody else's homework?

You are not really helping him, besides,
all the answers are completely false!
No, answers are ok.


Jul 16 '06 #8

P: n/a
dbz
Why do you do somebody else's homework?
>
You are not really helping him, besides,
all the answers are completely false!

jacob

Dude jacob, 1st of all i am not trying to do my HW. If some1 asks a
question, is that always supposed to be his HW?

2ndly, if you dont want to help people, why dont you just keep quite
and go away??

-Regards

Jul 16 '06 #9

P: n/a
"dbz" <pu**************@gmail.comwrites:
>Why do you do somebody else's homework?

You are not really helping him, besides,
all the answers are completely false!

jacob


Dude jacob, 1st of all i am not trying to do my HW. If some1 asks a
question, is that always supposed to be his HW?

2ndly, if you dont want to help people, why dont you just keep quite
and go away??
Google should have provided a line that looked something like this:

jacob navia <ja***@jacob.remcomp.frwrites:

That's called an attribution line. Please don't delete it. It helps
us to follow the discussion.

The questions you asked looked very much like a homework assignment,
and we do get a lot of people here who post questions that boil down
to "Please do my homework for me because I'm too lazy." (One common
response is to ask for the instructor's e-mail address so we can
submit our solutions directly.)

If the questions you posted weren't a homework assignment, it would be
helpful if you said so. We can give better answers if we know why
you're asking. For example, if we know what problem you're trying to
solve, suggesting a different approach can often be more useful than
answering the wrong question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 16 '06 #10

P: n/a
Eric Sosman wrote:
Joe Wright wrote:
>dbz wrote:
>>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)
Nothing yet. You have declared a structure and defined a pointer to
it, but you don't have a structure yet.

p = malloc(sizeof *p);

Now, assuming success of malloc, p points to a structure and p[0] is
the structure.

1. p->word is just fine.
2. p->n or (*p).n or p[0].n
3. (*p).word or p[0].word
4. *(p->word) is just fine.

Don't all four yield undefined behavior for the
use of indeterminate values?
Indeed. Values of p->word and p->n are indeterminate.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 18 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.