1. ## Array of structers

In my book a question is like below:
Match the following with reference to the following program segment:
Code:
struct
{
int x, y;
}s[]={10, 20,15,25,8,75,6,2};
int *i;
i=s;

 1 *(i+3) a 85 2 s[i[7]].x] b 2 3 s[(s+2)->y/3[I]].y c 6 4 i[i[1]-i[2]] d 7 5 i[s[3].y] e 16 6 (s+1)->x+5 f 15 7 *(1+i)**(i+4)/*i g 25 8 s[i[0]-i[4]].y+10 h 8 9 (*(s+*(i+1)/*i)).x+2 i 1 10 ++i[i[6]] j 100 11 k 10 12 l 20

My doubts are, is it to declare *i as int and assign like i=s;
and division with pointer gives error in compiler.

2. Originally Posted by justine
My doubts are, is it to declare *i as int and assign like i=s;
This is fine - it's called pointer decay. In this case, use of the variable "s" (which has an array type) decays to a pointer to that array.

Originally Posted by justine
and division with pointer gives error in compiler.
On one hand, this is how the compiler should behave - division with a pointer is not a thing you can do. However, I don't see any division involving pointers - what am I missing?

3. The assignment "i = s" is invalid, since i is a pointer to int, but s is an array of a struct type.

A compiler will therefore reject the code, and all of the entries in the table are therefore meaningless.

4. However, I don't see any division involving pointers - what am I missing?
Code:
*(1+i)**(i+4)/*i
(*(s+*(i+1)/*i)).x+2
A compiler will therefore reject the code
It is not rejecting, but gives warning.
untitled1.c:9:3: warning: assignment from incompatible pointer type [enabled by default]

5. Originally Posted by justine
Code:
*(1+i)**(i+4)/*i
(*(s+*(i+1)/*i)).x+2
You're not dividing pointers here - the first involves "/*i", which is a pointer that's dereferenced to an int, and in the second operator precedence means you're adding "s" to "*(i+1)/*i".

But grumpy's already given you the reason that the compiler's rejecting this code.

6. As long as the first line is corrected to i = (int *)&s, the answers are: 25, 8, 2, 75, 15, 20, 16, 85, 10, 2.

7. Code:
#include <stdio.h>
int main()
{
struct
{
int x, y;
}s[]={
{10, 20},
{15,25},
{8,75},
{6,2}
};
int *i;
i=(int*)&s;
printf("%d", *(1+i)**(i+4)/*i);
return 0;
}
Still rejecting:
gcc -Wall -o "untitled1" "untitled1.c" (in directory: /home/duh/Desktop/C)
untitled1.c: In function ‘main’:
untitled1.c:15:36: error: unterminated comment
untitled1.c:15:10: error: expected ‘)’ at end of input
untitled1.c:15:10: error: expected declaration or statement at end of input
untitled1.c:15:10: warning: control reaches end of non-void function [-Wreturn-type]
Compilation failed.
How to fix it?
Thanks

8. That is because the compiler interpreted /* as the start of a comment. You should space out your code, e.g.,
Code:
#include <stdio.h>

int main(void)
{
struct
{
int x, y;
} s[] = {{10, 20}, {15,25}, {8,75}, {6,2}};
int *i;
i = (int*)&s;
printf("%d", *(1 + i) * *(i + 4) / *i);
return 0;
}

9. Ok this is working

10. By the way, instead of "array of structers", we would normally say "array of objects of a struct type" which may be abbreviated to "array of struct objects" or even "array of structs".

11. Here what is the use of "s", I think s[0] is the base address.

Edit: Nope 's' is, How did that happen. Is it two dimensional?

12. Originally Posted by laserlight
By the way, instead of "array of structers", we would normally say "array of objects of a struct type" which may be abbreviated to "array of struct objects" or even "array of structs".
So there is other thing with name array of structers?

13. Originally Posted by justine
Here what is the use of "s", I think s[0] is the base address.

Edit: Nope 's' is, How did that happen. Is it two dimensional?
s is not an address and neither is s[0]. s is an array, s[0] is a struct object, where the struct type is anonymous. The notation that you are looking at is used to initialise an aggregate, in this case an array of struct objects. It happens to be the same notation that you might use to initialise a 2D array, but that does not mean that s is a 2D array.

Originally Posted by justine
So there is other thing with name array of structers?
Not that I know of.

14. Thanks laserlight

15. Originally Posted by laserlight
That is because the compiler interpreted /* as the start of a comment. You should space out your code