tags:

views:

125

answers:

5

The structure defined below does not have a structure name. Moreover I am unable to get the array part. Thanks 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;
}
+1  A: 

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.

vulkanino
No, it's syntactically fine; the only thing that I would call an actual *bug* in this code is the pointer arithmetic
Zack
I guess it won't even complile for the pointer assignment, but if he casts like i = (int*) s; then it will maybe print... 15?:p crap
vulkanino
It wasnt even compiling
fahad
+1  A: 

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!

tommieb75
The inner braces are not required.
Zack
@Zack: The braces are used to distinguish the structs in themselves within the initialization.... unless correct me?
tommieb75
don't think this is correct. nameless structs are fine. your code is wrong, you should declare mystructTag mystructlist[]=...
vulkanino
@tommieb75: Yes, that's what the inner braces do, and it's good style to put them there, but you can leave them out if you want. The OP's code compiles fine (well, with warnings, and an undefined reference to `clrscr`, but that's not really the point here).
Zack
also, your code fragment declares two objects ('mystruct', uninitialized, and 'mystructlist', initialized), is that really what you meant? And why the inconsistent spacing in the initializer list?
Zack
@Zack: sure.... I posted a comment above under the question about casting an int pointer to array of structs..... as for the inconsistent spacing that...can be easily fixed... :)
tommieb75
This code [doesn't even compile](http://www.ideone.com/nfsA4). You need `struct mystructTag` on the last line, this is c, not c++. And why are declaring a useless extra object here (`mystruct`)?
bstpierre
+1  A: 

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;
   }
Xinus
+3  A: 

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.

Zack
+1  A: 

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.

BillP3rd