A Query in "Pointers to Structures"

D

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)
 
T

Tosha

dbz said:
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.
 
A

August Karlstrom

dbz said:
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
 
J

Joe Wright

dbz said:
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.
 
E

Eric Sosman

Joe said:
dbz said:
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?
 
J

jacob navia

Tosha a écrit :
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
 
R

Rod Pemberton

dbz said:
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 *((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
 
T

Tosha

jacob navia said:
Tosha a écrit :

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.
 
D

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
 
K

Keith Thompson

dbz said:
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:


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.
 
J

Joe Wright

Eric said:
Joe said:
dbz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top