473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A Query in "Pointers to Structures"

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
10 1563

"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
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
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
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
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

"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

"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robin Tucker | last post by:
Hi, I'm currently implementing a database with a tree structure in a table. The nodes in the tree are stored as records with a column called "Parent". The root of the tree has a "NULL" parent....
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
24
by: arnuld | last post by:
PURPOSE: see the comments. WHAT I GOT: infinite loop /* This program will simply create an array of pointers to integers * and will fill it with some values while using malloc...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.