473,778 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1594

"dbz" <pu************ **@gmail.comwro te in message
news:11******** **************@ 75g2000cwc.goog legroups.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.comwro te in message
news:11******** **************@ 75g2000cwc.goog legroups.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.comwro te in message
news:11******** **************@ 75g2000cwc.goog legroups.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.re mcomp.frwrote in message
news:44******** *************@n ews.orange.fr.. .
Tosha a écrit :
>"dbz" <pu************ **@gmail.comwro te in message
news:11******* *************** @75g2000cwc.goo glegroups.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.comwri tes:
>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.re mcomp.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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1986
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. The path to each node is stored in the column "Path" and is of the form "\000001\000002\000003\" etc. The latter enabling me to fetch subtrees using the "LIKE" predicate. I also have created the relation "ID" <-> "ID_Parent, effectively the...
388
21929
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
46
2270
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 to send scanf the address of.." Isn't the lvalue called a POINTER TO and the (r)value called the ADDRESS OF?
14
2838
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 contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
24
2195
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 to create * pointers to fill the array and then will print the values pointed * by those pointers *
0
10292
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.