424,856 Members | 1,980 Online
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 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 Replies

 P: n/a "dbz" 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->word2) (*p)->n3) (*p)->word4) *(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" >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->word2) (*p)->n3) (*p)->word4) *(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" 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" "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->word2) (*p)->n3) (*p)->word4) *(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 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" 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 San Diego Supercomputer Center <* 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->word2) (*p)->n3) (*p)->word4) *(p->word) Nothing yet. You have declared a structure and defined a pointer toit, 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] isthe structure.1. p->word is just fine.2. p->n or (*p).n or p[0].n3. (*p).word or p[0].word4. *(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.