The structure defined below does not have a structure name. Moreover I am unable to get the array part. Tha开发者_Python百科nks in advance. What does the values in the array of structure mean?
#include <stdio.h>
int main()
{
struct
{
int x,y;
} s[] = {10,20,15,25,8,75,6,2};
int *i;
i = s;
clrscr();
printf("%d\n",*(i+3));
return 0;
}
Now that we have a question that can be answered:
struct
{
int x,y;
} s[] = {10,20,15,25,8,75,6,2};
This declares an array named s
, of anonymous structures. Each structure has two int
fields. The array size is not specified, so it'll be as big as it needs to be to hold the initializer list ("initializer list" is the technical term for the brace-enclosed list of numbers after the equals sign).
The code in your textbook is sloppy; it's not marking off sub-structures in the initializer list; this is allowed, but is considered poor style. The compiler interprets it exactly the same as if the code had been like this:
struct
{
int x,y;
} s[] = { {10,20}, {15,25}, {8,75}, {6,2} };
Each brace-enclosed pair of numbers initializes one element of the array s
. The first number initializes x
and the second y
. So, all these are true:
s[0].x == 10
s[0].y == 20
s[1].x == 15
s[1].y == 25
and so on.
Whenever you do require to refer struct
not more than once it makes sense creating variable of it without giving name to it.
In C syntax of achieving it is
struct{
/*
.
.your structure members
.
*/
}<variable name>;
in your case s
is a array of
struct
{
int x,y;
}
You can have a nameless-struct, but of course you can't reuse it's type. s is an array of structures, initialized directly in the declaration.
I doubt it is correct syntax though.
It's not entirely clear what you're expecting to see as output from the code you've posted. If you expect to see "25", then you're right on. Accessing members of your structure the way you're attempting to do, while "valid" in this particular case, can be very hazardous unless you know exactly what you're doing. Consider the following minor change to your structure:
struct
{
int x;
char y;
} s[] = {10,20,15,25,8,75,6,2};
What do you think your code would get now? The simple answer is "it depends." On what, you ask? On the size of a char
in your chosen system's architecture. It's usually 8 bits, but that's not guaranteed. ANSI C and C99 define char
as follows:
"An object declared as type
char
is large enough to store any member of the basic execution character set."
It also depends on how your particular compiler "aligns" the members of the structure. The compiler will generally align members on word boundaries but, again, what that means depends on the size of a "word." The structure could also be aligned on byte boundaries. The point is, there are better ways to access data in structures. Consider:
#include <stdio.h>
int main()
{
struct
{
int x, y;
} *ps, s[] = { {10, 20}, {5, 25}, {8, 75}, {6, 2} };
ps = s;
printf( "%d\n", *(ps + 3) );
printf( "%d\n", s[3].x );
printf( "%d\n", (ps + 3)->x );
return 0;
}
In this example, ps
is declared as a pointer to the unnamed structure. After making the assignment ps = s
, ps
points to the first structure member. If you add 1 to ps
, as in ps += 1;
, it will point to the second structure member, and so forth. So, in the example, all three printf
statements print the number 6. Can you see why? In the first case, *(ps + 3)
, as demonstrated when we add to ps
, we're advancing the pointer through the array of structures so it points to the third element. In the second case, s[3].x
, we're referring the the third element in the array of structures s
, and taking the value of x
in that element. Finally, (ps + 3)->x
, we add three to ps
as before then, dereferencing it as a pointer to our struct type, we take the value of x
.
Also note the way I've initialized s
. While the inner braces are not required, they're good form. If you had a more complex structure, it would be very hard to do a correct initialization without the braces to help as a guide, and for someone following after, it would be impossible to read.
in your struct declaration
that should read
struct mystructTag
{
int x,y;
}mystruct;
mystructTag mystructlist[]={ {10,20}, {15,25}, {8,75}, {6,2} };
Thanks to vulkanino for pointing out a little blooper!
精彩评论