1. ## Structure

Can anyone please explain what var1 is in the following?

A sructure i.e. struct ABC
{
int x;
int y;
} ABC_1;

Variable declared as: static ABC_1 var1;

2. ## Its just a variable

Hi there,
let me try to explain what var1 is...

When You Say

int i;

you inform the compiler to reserve memory (as much as needed for an integer which can be two or four bytes depending on the environment you are using) that can hold an integer value and assign it the name 'i'.

Now, you have declared a Structure (user defined data type). So, you want to store three different data-items in three different variables but want them to be bound by a common name. So you have created a data type. Next step requires that you declare a variable of that data type (just like int i). Here is the problem, since the C compiler does not recognise the user-defined datatypes as standard, you inform this by saying...
struct ABC <<variable_name>>;

This reserves the memory required by the variables of the structure ABC and assigns the common name var1 to them. So, you can then refer to individual elements using the '.' notation like...
var1.x
var1.y
etc...
Hope you catch the point,,,

Regards,

3. Wait Wait....
Im really sorry I did not go through ur question properly...
Letme check, and I'll get back to u again...
Sorry again.

Regards,

4. Well, you are declaring a variable of type "ABC_1", and it is persistent only to the scope of the file it's in.

Code:
```typedef struct                           /* create new type - the correct way */
{
int x;
int y;
}ABC_1;

static ABC_1 var1;                    /* declare a variable of the new type */

int foo(*ABC_1);                       /* prototypes */
int main(void);

/* funcs */

int main(void)
{
int equality = 0;                     /* init vars */

var1.x = 5;                             /* init fields in struct */
var1.y = 5;

equality = foo(&var1);           /* pass struct by reference */

return(0);
}

int foo(*ABC_1 v)
{
if(v->x == v->y)                     /* compare fields in struct */
return(1);
else
return(0);
}```
this shows it to you and how it might be used.

Popular pages Recent additions